1

I am getting Forbidden Attributes Error even though I have used strong parameters in Rails. I have two models: Posts and Categories. Here is my Post Controller: class PostsController < ApplicationController def index @posts=Post.all end

def new
    @post=Post.new
    @category=Category.all
end

def create
    @post = Post.new(params[:post])
    if @post.save
        redirect_to posts_path,:notice=>"Post saved"
    else
        render "new"
    end
end

def allowed_params
    params.require(:post).permit(:title, :body, :category_id)
end 

end

And here is my view for posts/new:

<%= form_for @post do |f| %>
    <p>
        <%= f.label :title %></br>
        <%= f.text_field :title%><br/>
    </p>
    <p>
        <%= f.label :body %></br>
        <%= f.text_area :body%><br/>
    </p>
    <p>
        <%= f.select :category_id, Category.all.collect{|x| [x.name,x.id]},{:include_blank =>"Select one"}%><br/>
    </p>
    <p>
        <%= f.submit "Add Post" %>
    </p>
<% end %>

But I am still getting Error.

1 Answer 1

3

You need to use allowed_params instead of params[:post]:

@post = Post.new(allowed_params)

1
  • Thanks. Worked for me
    – Rajat
    Sep 24, 2017 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.