Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 预览页面注册重定向到其他静态页面错误_Ruby On Rails_Ruby_Ruby On Rails 4_Rails Routing - Fatal编程技术网

Ruby on rails 预览页面注册重定向到其他静态页面错误

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

我有一个预览页面,上面有一个接收电子邮件的表单(@premails)。我已经为此创建了一个模型&迁移

我有一个页面控制器,带有主页、关于和联系人操作以及相应的视图

在他们在主页上提交电子邮件后,我想将他们重定向到一个静态的关于页面。我一直未能做到这一点

这是我的页面控制器:

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]