轨道上的红宝石 - 如何验证主键



我无法验证Transaction_Id这是我的交易表的主键,而我可以用email来验证。 似乎有什么问题?帮助

。 谢谢。

这是我的模型/交易:

def self.authenticate(email, transaction_id)
    user = Transaction.find_by_email(email)
    if user && user.Transaction_Id
      return user
    else
      return false
    end
end

这是我的控制器/修改:

  def attempt_login
    user = Transaction.authenticate(params[:email], params[:Transaction_Id])
    if user
        session[:user_id] = user.id
        session[:email] = user.email
        flash[:notice] = "You are now logged in!"
        redirect_to :action => "modify"
    else
        flash[:notice] = "Invalid username/password  combination."
        redirect_to :action => "login"
    end
  end

这是我的观点/登录:

<div class="login">
    <%= form_tag :action => "attempt_login" do %>
        <%= label_tag :email, "Email Address:" %>
        <%= text_field_tag :email %>
        <%= label_tag :Transaction_Id, "Transaction Id:" %>
        <%= text_field_tag :Transaction_Id %>
        <%= submit_tag "Log In"%>   
    <% end %>
</div>

仅检查找到的用户对象中是否存在transaction_id,但不将此 id 与给定的 id 进行比较,因此请尝试:

def self.authenticate(email, transaction_id)
    user = Transaction.find_by_email(email)
    if user && user.Transaction_Id == transaction_id
      return user
    else
      return false
    end
end

或(不知道它是否有效)

def self.authenticate(email, transaction_id)
    user = Transaction.find_by_email_and_transaction_id(email, transaction_id)
    if user
      return user
    else
      return false
    end
end

总之:

# will return user if found; else nil
def self.authenticate(email, transaction_id)
    Transaction.find_by_email_and_transaction_id(email, transaction_id)
end

相关内容

  • 没有找到相关文章

最新更新