Ruby on rails 预览页面注册重定向到其他静态页面错误
我有一个预览页面,上面有一个接收电子邮件的表单(@premails)。我已经为此创建了一个模型&迁移 我有一个页面控制器,带有主页、关于和联系人操作以及相应的视图 在他们在主页上提交电子邮件后,我想将他们重定向到一个静态的关于页面。我一直未能做到这一点 这是我的页面控制器:Ruby on rails 预览页面注册重定向到其他静态页面错误,ruby-on-rails,ruby,ruby-on-rails-4,rails-routing,Ruby On Rails,Ruby,Ruby On Rails 4,Rails Routing,我有一个预览页面,上面有一个接收电子邮件的表单(@premails)。我已经为此创建了一个模型&迁移 我有一个页面控制器,带有主页、关于和联系人操作以及相应的视图 在他们在主页上提交电子邮件后,我想将他们重定向到一个静态的关于页面。我一直未能做到这一点 这是我的页面控制器: class PagesController < ApplicationController def home @premail = Premail.new if @premai
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
class PagesController
但当我用以下代码打开本地主机时,我得到:
NameError in PagesController#home
undefined local variable or method `about_path' for #<PagesController:0x337ac40>
页面控制器主页中的name错误
未定义的局部变量或方法“about_path”#
我怎样才能做到这一点 对于您的情况,请使用:
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
中没有:
match ':controller(/:action(/:id))'
在Rails 4中:
match ':controller(/:action(/:id))', :via => [:get , :post]
如果您计划只回答“获取”,即没有表格发布到控制器:
get ':controller(/:action(/:id))'
这将检测诸如localhost:3000/asd/qwe/1
和:
asd
作为控制器asd控制器
qwe
作为操作:
class AsdController
def qwe; end
参数[:id]
将等于1()
表示可选,例如,如果您在浏览器中转到localhost:3000/asd
,Rails将调用asd#index
,即:
class AsdController
def index
# whatever you have here
end
我有这个“资源:页面”这不管用吗?有没有更好的方法不必这样做,我听说使用“资源”语法更好。。在不修改其他代码以重定向到about页面的情况下,我可以将什么格式传递到'redirect_to'?我已经完成了所有这些操作,并得到以下错误:没有路由匹配{:action=>“about”,:controller=>“pages”}为什么需要这样做?我从没见过这个。。。它做什么?顺便说一句,这给了我另一个错误:如果没有指定HTTP方法,您不应该在路由器中使用
match
方法。如果要将操作同时公开给GET和POST,请通过:[:GET,:POST]选项添加。如果您想公开要获取的操作,请在路由器中使用GET
:而不是:match“controller”#action“Do:GET”controller#action啊,对了,在Rails 4中,通用match
调用是不允许的,您可以使用GET更改匹配。但是如果您有一个表单,您可以执行:match':controller…,:via=>[:GET,:post]