提问者:小点点

Rails 5中的禁止属性错误


即使我在Rails中使用了强参数,我也会收到禁止属性错误。我有两种模型:帖子和类别。这是我的帖子控制器:类PostsController

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 

结束

以下是我对帖子/新的看法:

<%= 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 %>

但我仍然得到错误。


共1个答案

匿名用户

您需要使用allowed_params而不是params[: post]

@post=Post.new(allowed_params)