mohangraj
mohangraj

Reputation: 11034

Setting default page for Controller in Rails

In rails how to set the default page for controller. In my application I have a controller named "greet" which have two actions "welcome" and "wishes". So while calling the welcome page like "localhost:3000/greet/welcome" is properly worked.

But My requirement is if I didn't give the action name for that controller like "localhost:3000/greet", then it takes the default page associated for that controller only. How to do this in rails 4.2. I tried to make an index action within greet controller. But it didn't work. Can anyone help me to solve this problem ?

Upvotes: 1

Views: 475

Answers (4)

Nimir
Nimir

Reputation: 5839

Rails.application.routes.draw do
  resource :greet, controller: 'greet' do
    get 'welcome'
    get 'wishes'

    #Default resource routing
    get '/', to: 'greet#welcome'
  end
end

Upvotes: 1

Prashant Ravi Darshan
Prashant Ravi Darshan

Reputation: 545

Try this.

 get '/greet', to: 'greet#welcome'

Upvotes: 1

Manish Shrivastava
Manish Shrivastava

Reputation: 32040

Rails work with REST concept. So, according to this when you just call localhost:3000/greet it will search greet#index method. Well, If you want to see any custom method while usinglocalhost:3000/greet, you will need to write in file config/routes.rb like:

Rails.application.routes.draw do
  get 'greet', :to => 'greet#welcome', :as => :greet
end

Hope this will help.

Upvotes: 1

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

in your routes.rb add line:

get '/greet' => 'greet#welcome'

you must also in folder view create folder greet and in this folder you have to create file welcome.html.erb

Upvotes: 1

Related Questions