【问题标题】:Rails Action Not Handling RedirectRails 操作不处理重定向
【发布时间】:2015-03-29 04:13:01
【问题描述】:

我正在尝试创建一个从动作“other_hello”到动作“index”的简单重定向。现在,当我使用 url “localhost:3000/demo/other_hello”从浏览器发出 GET 请求时,它正在重定向到主页。我希望它被重定向到索引操作,然后呈现“演示/你好”视图。我当前的控制器如下所示:

class DemoController < ApplicationController
  layout false

  def index
     render('demo/hello')
  end

  def hello
     render('demo/index')
  end

  def other_hello
     redirect_to(:controller => 'demo', :action => 'index')
  end

end

我当前的文件夹结构如下:

[root@vc2cmmkb036933n simple_cms]# tree
.
├── app
│   ├── assets
│   │   ├── images
│   │   │   └── rails.png
│   │   ├── javascripts
│   │   │   ├── application.js
│   │   │   └── demo.js
│   │   └── stylesheets
│   │       ├── application.css
│   │       └── demo.css
│   ├── controllers
│   │   ├── application_controller.rb
│   │   └── demo_controller.rb
│   ├── helpers
│   │   ├── application_helper.rb
│   │   └── demo_helper.rb
│   ├── mailers
│   ├── models
│   └── views
│       ├── demo
│       │   ├── hello.html.erb
│       │   └── index.html.erb
│       └── layouts
│           └── application.html.erb
├── config
│   ├── application.rb
│   ├── boot.rb
│   ├── database.yml
│   ├── environment.rb
│   ├── environments
│   │   ├── development.rb
│   │   ├── production.rb
│   │   └── test.rb
│   ├── initializers
│   │   ├── backtrace_silencers.rb
│   │   ├── inflections.rb
│   │   ├── mime_types.rb
│   │   ├── secret_token.rb
│   │   ├── session_store.rb
│   │   └── wrap_parameters.rb
│   ├── locales
│   │   └── en.yml
│   └── routes.rb
├── config.ru
├── db
│   └── seeds.rb
├── doc
│   └── README_FOR_APP
├── Gemfile
├── Gemfile.lock
├── lib
│   ├── assets
│   └── tasks
├── log
│   └── development.log
├── public
│   ├── 404.html
│   ├── 422.html
│   ├── 500.html
│   ├── demo
│   │   └── test.html
│   ├── favicon.ico
│   ├── index.html
│   └── robots.txt
├── Rakefile
├── README.rdoc
├── script
│   └── rails
├── test
│   ├── fixtures
│   ├── functional
│   │   └── demo_controller_test.rb
│   ├── integration
│   ├── performance
│   │   └── browsing_test.rb
│   ├── test_helper.rb
│   └── unit
│       └── helpers
│           └── demo_helper_test.rb
├── tmp
│   ├── cache
│   │   └── assets
│   │       ├── CF0
│   │       │   └── DA0
│   │       │       └── sprockets%2Fd7d5b37686831d37c4dd75e645f5e016
│   │       └── E25
│   │           └── 4C0
│   │               └── sprockets%2Fde2fd9fd11c04a582cdbbe3d84a35ae6
│   ├── pids
│   │   └── server.pid
│   ├── sessions
│   └── sockets
└── vendor
    ├── assets
    │   ├── javascripts
    │   └── stylesheets
    └── plugins

47 directories, 50 files

为了格外小心,我的 routes.rb 文件如下所示:

SimpleCms::Application.routes.draw do
  # get "demo/index"
  root :to => "demo#index"

  match ':controller(/:action(/:id))', :via => :get
end

我目前使用的是 Ruby 1.9.3 和 Rails 3.2.3。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 http-redirect


    【解决方案1】:

    试试这样:--

    class DemoController < ApplicationController
      layout false
    
      def index
         render :action => 'hello'
      end
    
      def hello
         render :action => 'index'
      end
    
      def other_hello
       redirect_to :action => :index 
      end
    
    end
    

    【讨论】:

      【解决方案2】:

      这很正常:当您使用路由选项哈希调用 redirect_to 时,rails 首先必须使用您的路由文件将这些选项转换为路径。

      路由文件中与 controller=demo 和 action=index 匹配的第一个条目是您的 root 条目,因此生成的路径是“/”

      如果你要添加

      get "demo/index"
      

      在路由文件的较高位置(例如取消注释),您应该会得到您期望的路径(尽管您现在有 2 个 url 映射到同一页面)。使用文件底部的全部路径是相当过时的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-18
        • 2018-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多