Search code examples
ruby-on-railsscaffolding

Rails model scaffolding not working properly


In a very simple rails project from a tutorial I run rails generate scaffold Event title:string date:date description:text. Everything is ok except that when I open the EventsController.rb I see that the show method is empty:

def show
end

In the video of the course I see that this method is not supposed to be empty and should contain things like @event = Event.find(params[:id]). What could possibly be the problem?


Solution

  • In Rails 4 the style is changed to getting particular resource based on id. So it is moved to before_folter section.

    You controller may be like,

    before_action :set_event, only: [:show, :edit, :update, :destroy]
    
    def set_event
      @event = Event.find(params[:id])
    end
    

    Please see the controller private methods.