如何validation主键?
我无法validationTransaction_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
这是我的观点/登录:
"attempt_login" do %>
您只检查找到的用户对象中是否存在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
- 保存模型时删除关联的记录
- EventMachine和Ruby Threads – 这里到底发生了什么?
- Rails,将文件上传到服务器时获取文件名
- Rails,activerecord:self vs self.attribute
- 如何在Ruby on Rails中为所有模型动态更改Active Record数据库?
- Rails 4:heroku基本计划上的ConnectionTimeoutError
- CSS没有在Heroku中加载
- rails activerecord,friend relation + inverse_friend关系如何获得相互关系? 代码包括在内
- Rails 4.2 – dependent :: restrict_with_error – 访问错误