[英]ActiveModel::ForbiddenAttributesError Ruby On Rails
[英]Rails ActiveModel::ForbiddenAttributesError
rarils 4.0.0,我正在嘗試發表評論,但出現錯誤:
CommentsController#create ActiveModel :: ForbiddenAttributesError中的ActiveModel :: ForbiddenAttributesError
def create
@comment = @article.comments.new(params[:comment]) #error point highlight this line
Parameters
{"utf8"=>"✓",
"authenticity_token"=>"zSq3KpEbucFQLa6XStEJ/I0+CpKPLFYcU/WGIdneeMg=",
"comment"=>{"name"=>"g12345",
"email"=>"g12345@12345.com",
"body"=>"hello hello"},
"commit"=>"Add",
"article_id"=>"5"}
我的評論/new.html.erb
<%= form_for([@article, @article.comments.new], remote: true) do |f| %>
<%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit 'Add' %>
</div>
<% end %>
Rails 4默認使用強參數。 你有類似的東西嗎?
params.require(:some_param).permit(...)
要么
params.permit(:list, :of, :allowed, :params)
在您的CommentsController
?
它看起來像這樣:
class CommentsController < ApplicationController
def create
@comment = @article.comments.new(comment_params) #error point highlight this line
end
private
def comment_params
params.require(:comment).permit(:name, :email, :body)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.