RailsCasts Pro episodes are now free!

Learn more or hide this

vgracia's Profile

GitHub User: vgracia

Comments by

Avatar

I'm surprised there were no comments about issues with adding an entry. This took me sometime to figure out so posing here.

I was getting a 500 error when trying to add a name entry:

Completed 500 Internal Server Error in 1ms

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
  app/controllers/entries_controller.rb:13:in `create'

I guess in rails 4 some extra parameter requirements are needed:
more info here:
http://stackoverflow.com/questions/17868427/rails-4-activemodelforbiddenattributeserror
http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

Here's my implementation of the fix. Let me know if I overlooked something and this is not really needed please!

In entries_controller.rb

ruby
class EntriesController < ApplicationController
  respond_to :json
  
  def index
    respond_with Entry.all
  end
  
  def show
    respond_with Entry.find(params[:id])
  end
  
  def create
    respond_with Entry.create(entry_params)
  end
  
  def update
    respond_with Entry.update(params[:id], params[:entry])
  end
  
  def destroy
    respond_with Entry.destroy(params[:id])
  end

  private
    # permissible attributes.
    def entry_params
      params.require(:entry).permit(:name)
    end

end
Avatar

Uhg... spent so much time trying to figure this out ... THANKS!

I am new to rails/backbone so it was pretty confusing.