[英]Rails 3 'new' action as the index page due to a form
I have a small app where the index page is a form that instructs the user for their email, I have changed my route so that the root_path is the 'new' action which renders my form: 我有一个小应用程序,其中索引页面是一个指示用户的电子邮件的表单,我已经改变了我的路线,以便root_path是呈现我的表单的“新”操作:
Oa::Application.routes.draw do
resources :signups
match "/confirm", :to => "pages#confirm"
root :to => 'signups#new'
end
This is working fine, when I submit the form it is working fine. 这工作正常,当我提交表格它工作正常。 When I submit on the root page and I cause a validation error the address bar has the url localhost:3000/signups and shows me my validation errors, which is also good but if I were to manually visit http://localhost:3000/signups it gives me an error "The action 'index' could not be found for SignupsController". 当我在根页面上提交并导致验证错误时,地址栏上有url localhost:3000 / signups并显示我的验证错误,这也很好但是如果我要手动访问http:// localhost:3000 /注册它给我一个错误“无法找到SignupsController的动作'索引'”。 Would it be ok if I created the 'index' action and redirect_to root_path so that I don't receive the "The action 'index' could not be found for SignupsController" if I were to access http://localhost:3000/signups directly? 如果我创建了'index'动作并且redirect_to root_path以便我没有收到“为SignupsController找不到动作'索引”,如果我要访问http:// localhost:3000 / signups,那会没关系吗?直? Is this the proper way to do this? 这是正确的方法吗?
class SignupsController < ApplicationController
def index
redirect_to root_path
end
def new
@signup = Signup.new
end
def create
@signup = Signup.new(params[:signup])
if @signup.save
UserMailer.registration_confirmation(@signup).deliver
flash[:notice] = "Signup created successfully."
redirect_to confirm_path
else
render :action => "new"
end
end
end
Thanks! 谢谢!
J Ĵ
Do something like this: 做这样的事情:
post '/' => 'signups#create'
root to: 'signups#new'
Do not use resources :signups
不要使用resources :signups
The reason that http://localhost:3000/signups gives me an error "The action 'index' could not be found for SignupsController" is because: http:// localhost:3000 / signups给我一个错误“无法找到SignupsController的动作'索引'的原因是因为:
The first 'matched' routes is used. 使用第一个“匹配”路线。
So all of this: 所有这一切:
match "/confirm", :to => "pages#confirm"
root :to => 'signups#new'
is ignored by the /signups URL because IT gets dealt with by 被/ signups URL忽略,因为IT处理了
resources :signups 资源:注册
which creates all the routes - index, show, create, new, edit, update, destroy 它创建了所有路径 - 索引,显示,创建,新建,编辑,更新,销毁
and when 'signups' is used it implies the signups index action. 当使用'signups'时,它意味着注册索引操作。
Try removing it and/or reading up on RESTful routes in rails and apply that. 尝试删除它和/或读取rails中的RESTful路由并应用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.