0
votes

I am trying to create a simple redirect from action "other_hello" to action "index". Right now, it is redirecting to the home page when I make a GET request from the browser using the url "localhost:3000/demo/other_hello". I want it to be redirected to the index action which then renders the "demo/hello" view. My current controller looks as follows:

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

my current folder structure is the following:

[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

And just to be extra careful my routes.rb file looks like:

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

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

I am currently using Ruby 1.9.3 and Rails 3.2.3.

2

2 Answers

0
votes

Try like that:--

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
0
votes

This is normal: when you call redirect_to with a hash of routing options rails first has to use your routes file to turn those options into a path.

The first entry in your routes file that matches controller=demo and action=index is your root entry so the generated path is '/'

If you were to add

get "demo/index"

Higher up in the routes file (for example uncomment it) then you should get the path you expect (although you now have 2 urls mapping to the same page). Using the catch all route that you have at the bottom of your file is rather old fashioned.