Q:

Add action to scaffold generated controller

I have created model, view and controller:
$ rails generate scaffold Post name:string title:string content:text

Then I have added the method on Post controller:
def fill_default_data
  Post.fill_default_data
end

But when I have open http://localhost:3000/posts/fill_default_data in browser I get the error:

ActiveRecord::RecordNotFound in
  PostsController#show
Couldn't find Post with
  ID=fill_default_data

It looks like Rails don't see fill_default_data action and use show method.  How can I add new method to scaffold generated controller?

A:

You should add the relevant route to your config/routes.rb file. If you currently have:
resources :posts

You should change that to:
resources :posts do
  collection do
    get :fill_default_data
  end
end

That will generate a route that you can access through /posts/fill_default_data. Now your app is actually accessing the show action and filling in "fill_default_data" as the ID.

