0

I am getting the ActiveModel::ForbiddenAttributesError with friendly_id and for the life of me cannot work out why. I believe I have configured my strong params as per normal and even tried modifying them multiple times to see what would happen.. I still get the error.

Please find the code below:

class Post < ActiveRecord::Base

    extend FriendlyId
    friendly_id :title, use: :slugged

    validates_presence_of :body, :title, :slug

    has_many :comments

end

class PostsController < InheritedResources::Base

    load_and_authorize_resource

    def new
        @post = Post.new
    end

    def create
        @post = Post.create(post_params)
        respond_to do |format|
            if @post.save
                format.html { redirect_to @post, notice: 'Post was successfully created.' }
                format.json { render :show, status: :created, location: @posts }
            else
                format.html { render :new }
                format.json { render json: @post.errors, status: :unprocessable_entity }
            end
        end

    end

    def show
        @post = Post.friendly.find(params[:id])
    end


    def index
        @posts = Post.all
    end

    def edit
    end

    def update
        if @post.update(post_params)
            redirect_to @post
        else
            render 'edit'
        end
    end

    private

    def post_params
        params.require(:post).permit(:title, :slug, :body)
    end


end

Does anyone know what's going on? I have looked over documentation but can't see anything. I have the field :slug added into my schema:

create_table "posts", force: true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "slug"
  end

  add_index "posts", ["slug"], name: "index_posts_on_slug", unique: true, using: :btree

As per comment request here is my form:

<div class='panel panel-default'>

    <div class='panel-heading'>
        <h2>New post</h2>
    </div>

    <div class='panel-body'>


        <div class="container">

            <%= semantic_form_for @post, :html => {:class => 'main-form'}  do |f| %>

            <div class='row'>

                <div class="col-xs-12">
                    <%= f.input :title, :input_html => { :class => 'form-control input-box', :placeholder => 'Title', :autofocus => true } %> 
                </div>

            </div>

            <div class='row'>

                <div class="col-xs-12">
                    <%= f.input :body, :input_html => { :class => 'form-control input-box', :placeholder => 'Body' } %>                     
                </div>

            </div>

            <div>
                <%= f.submit "Create Post", class: 'btn standard-button' %>
            </div>

            <% end %>

            <%= link_to 'Back', posts_path %>

        </div>

    </div>

</div>

Here is the error message:

ActiveModel::ForbiddenAttributesError - ActiveModel::ForbiddenAttributesError:                                                                                                                                                      
  activemodel (4.1.0) lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'                                                                                                                      
  activerecord (4.1.0) lib/active_record/attribute_assignment.rb:24:in `assign_attributes'                                                                                                                                          
  activerecord (4.1.0) lib/active_record/core.rb:452:in `init_attributes'                                                                                                                                                           
  activerecord (4.1.0) lib/active_record/core.rb:198:in `initialize'                                                                                                                                                                
  activerecord (4.1.0) lib/active_record/inheritance.rb:30:in `new'                                                                                                                                                                 
  activerecord (4.1.0) lib/active_record/inheritance.rb:30:in `new'                                                                                                                                                                 
  inherited_resources (1.4.1) lib/inherited_resources/base_helpers.rb:59:in `build_resource'                                                                                                                                        
  cancancan (1.9.2) lib/cancan/inherited_resource.rb:9:in `load_resource_instance'                                                                                                                                                  
  cancancan (1.9.2) lib/cancan/controller_resource.rb:32:in `load_resource' 

And this is the params:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JhF5bj4QU9Km0KfywdL0iCAZxczHd2MlF0FXSPtD47o=", "post"=>{"title"=>"asdfasdf", "body"=>"asdfasdf"}, "commit"=>"Create Post"}  
bnussey
  • 1,894
  • 1
  • 19
  • 34
  • Hey @smathy form added in, thanks for the request – bnussey Mar 31 '15 at 00:23
  • Ok, no clues there, can you add the full text of the error. – smathy Mar 31 '15 at 00:32
  • Hey @smathy I've added in the first part of the error message as it goes quite long.. fingers crossed you spot something! – bnussey Mar 31 '15 at 00:37
  • Sorry, it seems to be something triggered by that `InheritedResources` and/or `CanCanCan` stuff. I'm not sure at all how that's meant to be wired up. [A quick google uncovered this](https://github.com/CanCanCommunity/cancancan/wiki/FriendlyId-support) which I hope is what you're missing. – smathy Mar 31 '15 at 01:01
  • You mean you solved it with that information? – smathy Mar 31 '15 at 01:43
  • If so, you should write up the answer to your own question, and mark it as accepted so others who find this question will benefit. – smathy Mar 31 '15 at 01:44
  • 1
    Hey @smathy will do, just having some issues now with permissions, so will write it up once I've got it all working – bnussey Mar 31 '15 at 01:52

0 Answers0