Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4 ActiveModel::ForbiddenAttributesError [duplicate]

I would suggest skipping to update so your code works

Your problem is probably not in your controller, but your model: Check to see your attributes are accessible with the follow tag

attr_accessible :title, :price, :description

Rails 4 does do it a little different from what I understand, this previous SO answer provided some good links: How is attr_accessible used in Rails 4?

You need to use attr_accessible/Strong Params whenever you're accessing things from the database.

Update

Oh to be young, and not realize Rails 4 uses Strong Params. I understand the OP has already solved his original problem, but I'm going to correct this so it could actually be used as the right answer.

This would be a controller level issue, as Rails 4 requires you to whitelist attributes in the controller.

Ad.create(params[:ad].require(:ad).permit(:title, :price, :description))

Most of the time you'll be permitting the same params in the create and update action, so it's best to move it into its own method within the controller.

Ad.create(ad_params)
def ad_params
  params.require(:ad).permit(:title, :price, :description)
end

As OP pointed out in his comment, the permitted_params method he implemented was not being called from the permit.


I also faced same problem and worked for 6 hours, and finally got the solution.

Try this Code, it will help you! In Rails 4, this feature is added to deal with creation in different ways.

def new
  @ad = Ad.new
end

def create
  @ad = Ad.create(ad_params)
  @ad.save
end

private

def ad_params
  params.require(:ad).permit(:title, :price, :description)
end

Ad new before add params.permit!

   def create
     params.permit!
     @ad = Ad.new(params[:ad])    
     @ad.save
   end

Your's should look like this:

def new
    @ad = Ad.new(params[:ad].permit(:title, :price, :description))
end

Mine looks like this:

def create
    @about = About.new(params[:about].permit(:introduction, :description))

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