PostsController中的ActiveModel :: ForbiddenAttributesError #create

时间:2014-07-31 13:16:34

标签: ruby-on-rails

我刚刚创建了一个Rails应用程序来创建一个帖子,当我尝试创建一个帖子后使用rolify gem它在PostsController中创建了一个返回的ActiveModel :: ForbiddenAttributesError #create

::加载ActiveModel ForbiddenAttributesError

Post_controller.rb

  def create
  @post = Post.new(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: @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end 
end


private
  #Use callbacks to share common setup or constraints between actions.
  def set_post
   @post = Post.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def post_params
   params.require(:post).permit(:name, :content)
  end
end

Dint在表单中添加任何内容

form.html.erb

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being       saved:</h2>
      <ul>
        <% @post.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

如何解决?

0 个答案:

没有答案