cna*_*ize 1 ruby redirect ruby-on-rails simple-form
我是 Ruby on Rails 的菜鸟,我有一个愚蠢的问题:在我的 session_controller 中,我有
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_path, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
Run Code Online (Sandbox Code Playgroud)
在路线中,我匹配“/signin”,至:“sessions#new”
在 new.html.haml 我有
= simple_form_for :session do |f|
= f.input :email
= f.input :password
= f.button :submit, 'Sign in!'
Run Code Online (Sandbox Code Playgroud)
当我按下“登录”按钮时,我希望将调用“创建”方法(它在我的用户模型中的工作方式),但调试显示“新”方法,并且我有“authenticity_token”而无需填写电子邮件和密码。有人能给我解释一下发生了什么事吗?我不明白什么时候调用“new”方法,然后调用其他方法。
此行为与 无关simple_form。您没有为要调用的操作指定 url,也没有传递任何模型来推断其路径,因此您最终会得到url_for()在空哈希上调用的方法。此调用的结果是当前页面,这显然与显示表单的路径相同,但使用作为POSTHTML 动词。
要呼叫您sessions_path而不是您,new_session_path您可以执行以下操作:
= simple_form_for :session, url: :sessions do |f|
Run Code Online (Sandbox Code Playgroud)
通过 进行处理url_for,它实际上与指定相同,url: sessions_path但正在使您成为魔术师。
| 归档时间: |
|
| 查看次数: |
5225 次 |
| 最近记录: |