Here's how I have my login form set up:
以下是我设置登录表单的方法:
<%= form_tag login_path, method: "post", class: "well form-search" do |f| %>
Naturally, this tries to POST to the "new" action of my SessionsController. I know that login_path
is evaluated before method
so I know that this is completely wrong. Just trying to figure out how to make that login_path
do what I want. I know it is used for both GET and POST.
当然,这会尝试POST到我的SessionsController的“新”动作。我知道在方法之前评估login_path所以我知道这是完全错误的。只是想弄清楚如何使login_path做我想要的。我知道它用于GET和POST。
2 个解决方案
#1
I assume that you created a custom route called login
that goes to sessions#new
, but I recommend you add resources :sessions
to routes.rb
. That will give you all the routes you need and Rails will send you to the correct action in the controller.
我假设您创建了一个名为login的自定义路由,该路由转到了会话#new,但我建议您添加资源:sessions.rb的会话。这将为您提供所需的所有路由,Rails将向您发送控制器中的正确操作。
You'd have to update your form to:
您必须将表单更新为:
<%= form_tag sessions_path, class: "well form-search" do |f| %>
#2
You can do :
你可以做 :
<%= form_tag url: { action: "create", controller: 'sessions' }, method: "post", class: "well form-search" do |f| %>
#1
I assume that you created a custom route called login
that goes to sessions#new
, but I recommend you add resources :sessions
to routes.rb
. That will give you all the routes you need and Rails will send you to the correct action in the controller.
我假设您创建了一个名为login的自定义路由,该路由转到了会话#new,但我建议您添加资源:sessions.rb的会话。这将为您提供所需的所有路由,Rails将向您发送控制器中的正确操作。
You'd have to update your form to:
您必须将表单更新为:
<%= form_tag sessions_path, class: "well form-search" do |f| %>
#2
You can do :
你可以做 :
<%= form_tag url: { action: "create", controller: 'sessions' }, method: "post", class: "well form-search" do |f| %>