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.