1

I'm on Rails 4 and Ruby 2 and am trying to fix a bug that is giving me a Forbidden Attributes error when I'm creating a new record.

Here is my Create code in my controller:

def create
    @instructor = Instructor.new(instructor_params)

    respond_to do |format|
      if @instructor.save
        format.html { redirect_to @instructor, notice: 'Instructor was successfully created.' }
        format.json { render json: @instructor, status: :created, location: @instructor }
      else
        format.html { render action: "new" }
        format.json { render json: @instructor.errors, status: :unprocessable_entity }
      end
    end
  end

And my strong parameters code:

  def instructor_params
    params.require(:instructor).permit(:bio, :hometown, :name, :school, :sort_order, :started_sailing, :started_teaching, :photo)
  end

Here is my instructor.rb file:

class Instructor < ActiveRecord::Base
  validates_presence_of :bio, :name

  mount_uploader :photo, PhotoUploader

  def experience
     distance_of_time_in_words_to_now (self.started_sailing)
  end
end

The error I'm getting is:

ActiveModel::ForbiddenAttributesError in InstructorsController#create
ActiveModel::ForbiddenAttributesError

Rails.root: /Users/scottsipiora/Sites/clycss

Application Trace | Framework Trace | Full Trace
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"l2159VRjW9dUluWPJBxsubEt7t37CztXXVX/kjEpGfs=",
 "instructor"=>{"name"=>"Libby",
 "bio"=>"Bio content",
 "sort_order"=>"",
 "photo"=>#<ActionDispatch::Http::UploadedFile:0x007fd6aad96ec0 @tempfile=#<Tempfile:/var/folders/3k/fnl40lzs2v55jvj8yj6y07gh0000gn/T/RackMultipart20140706-8616-1mzzdxw>,
 @original_filename="Libby2.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"instructor[photo]\"; filename=\"Libby2.jpg\"\r\nContent-Type: image/jpeg\r\n">},
 "commit"=>"Create Instructor"}

I'm using cancan for authorization and found this page: https://github.com/ryanb/cancan/issues/835

I tried the workaround posted by AntonTrapp shown below, but that didn't work either.

before_filter do
  resource = controller_name.singularize.to_sym
  method = "#{resource}_params"
  params[resource] &&= send(method) if respond_to?(method, true)
end

Any idea what I'm doing wrong? I'm stumped.

Thanks in advance.

Scott S.
  • 749
  • 1
  • 7
  • 26
  • 2
    What is the exact error message? – BroiSatse Jul 06 '14 at 20:51
  • This is a pre-process error, irrelevant to strong params. It comes from your class, ie your model. Can you paste your `instructor.rb` file? – Ruby Racer Jul 06 '14 at 21:12
  • Please show us the workaround you used. I think the workaround by [lecky](https://github.com/ryanb/cancan/issues/835#issuecomment-21321676) is even cleaner and easier. – nathanvda Jul 06 '14 at 22:29
  • 1
    Related question: http://stackoverflow.com/questions/19273182/activemodelforbiddenattributeserror-cancan-rails-4-model-with-scoped-con/19504322#19504322 – nathanvda Jul 06 '14 at 22:31
  • minor comment - consider switching to new and updated gem CanCanCan – user1854802 Jul 07 '14 at 00:19

0 Answers0