如何验证主键?

时间:2011-05-13 11:29:34

标签: ruby-on-rails sql-server-2005 ruby-on-rails-3

我无法验证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>

1 个答案:

答案 0 :(得分:1)

您只检查找到的用户对象中是否存在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