我有一个预览页面,其中包含一个接收电子邮件的表单(@premails)。我创造了一个模型&为此迁移。
我有一个带有Home,About& amp的页面控制器。联系行动和相应的观点。
在主页上提交电子邮件后,我想将它们重定向到静态关于页面。我无法做到这一点。
这是我的页面控制器:
class PagesController < ApplicationController
def home
@premail = Premail.new
if @premail.save
redirect_to about_path
else
render home_path
end
end
def about
end
end
但是当我用这段代码打开我的localhost时,我得到了:
NameError in PagesController#home
undefined local variable or method `about_path' for #<PagesController:0x337ac40>
我怎样才能实现这一目标?
答案 0 :(得分:0)
对于您的情况,请使用:
if @premail.save
redirect_to :action => :about
end
此处不需要 else
,因为默认情况下Rails会呈现app/views/pages/home.html.erb
,请确保您拥有此文件。
当您重定向到about
时,您需要app/views/pages/about.html.erb
个文件。
对于Rails 3.x,似乎你在config/routes.rb
中没有this route:
match ':controller(/:action(/:id))'
在Rails 4中:
match ':controller(/:action(/:id))', :via => [:get , :post]
如果您打算回答获取,即向控制器发布或表格:
get ':controller(/:action(/:id))'
这将检测localhost:3000/asd/qwe/1
和
asd
作为控制器AsdController
使用qwe
作为操作:
class AsdController
def qwe; end
params[:id]
等于1.
()
表示可选,例如,如果您在浏览器中访问localhost:3000/asd
,则Rails会调用Asd#index
,即:
class AsdController
def index
# whatever you have here
end