【发布时间】:2017-03-03 09:09:45
【问题描述】:
在我的 RoR 应用程序中,我有一个 update_multiple 方法,它使用用户的输入更新多条记录。但是,由于某种原因,尽管使用了强大的参数,但我得到了错误 ActiveModel::ForbiddenAttributesError。有人可以帮我解决这个问题吗?
Recipients_Controller 中的update_multiple 方法如下:
def update_multiple
@email = Email.find_by_id(params[:email_id])
if Recipient.update(params[:recipient].keys, params[:recipient].values)
@listofcontacts = Recipient.where("id in (?)", params[:recipient].keys)
@account = Account.find_by_id(@email.account_id)
@listofcontacts.each do |f|
recipient_message = @email.message
recipient_message = recipient_message.gsub("VAR1", f.var1)
contact = Contact.find_by_id(f.contact_id)
@unsubscribe = Rails.application.message_verifier(:unsubscribe).generate(contact.id)
UserEmails.send_email(@email, @account, contact.email, @unsubscribe, recipient_message).deliver_now
end
flash[:notice] = "recipients were updated"
redirect_to root_path
else
render 'edit_multiple'
end
end
private
def recipient_params
params.require(:recipient).permit(:contact_id, :group_id, :email_id, :var1, :var2, :var3)
end
此方法从该表单获取用户输入:
<%= form_for :recipient, :url => update_multiple_recipients_path, :html => { :method => :put } do %>
<fieldset>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Contact</th>
<% if @email_message.upcase.include? "VAR1" %><th>VAR1</th><% end %>
</tr>
</thead>
<tbody>
<%= hidden_field_tag :email_id, @email %>
<% @recipients.each do |recipient| %>
<tr class="odd gradeX">
<%= fields_for "recipient[]", recipient do |recipient_fields| %>
<td><%= recipient_fields.label recipient.contact.firstname %> <%= recipient_fields.label recipient.contact.surname %></td>
<% if @email_message.upcase.include? "VAR1" %><td><%= recipient_fields.text_field :var1, :required => true, :maxlength => 20 %></td><% end %>
<% end %>
</tr>
<% end %>
</tbody>
</table></br>
<%= submit_tag 'Send Email', {:class => 'btn btn-primary'} %></br>
<%= link_to "Back", edit_email_path(@email) %>
</fieldset>
<% end %>
development.log 是这样写的:
Started PUT "/recipients/update_multiple" for ::1 at 2017-03-03 09:33:10 +0000
Processing by RecipientsController#update_multiple as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BJtQ56CW169tJ0Yqlc7BZNZk8SiTCauvkpNkXRUqVv4WESSS/DGFVDe3uQnfTxxDgif8lbg8THtmxHT9bOh0zw==", "email_id"=>"292", "recipient"=>{"635"=>{"var1"=>"ben"}}, "commit"=>"Send Email"}
[1m[36mEmail Load (0.0ms)[0m [1mSELECT "emails".* FROM "emails" WHERE "emails"."id" = ? LIMIT 1[0m [["id", 292]]
[1m[35mRecipient Load (1.0ms)[0m SELECT "recipients".* FROM "recipients" WHERE "recipients"."id" = ? LIMIT 1 [["id", 635]]
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35m (0.0ms)[0m rollback transaction
Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.0ms)
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
app/controllers/recipients_controller.rb:15:in `update_multiple'
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (1459.1ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (1449.1ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (3305.2ms)
上面写着app/controllers/recipients_controller.rb:15:in 'update_multiple',它指向if Recipient.update(params[:recipient].keys, params[:recipient].values)这一行
我真的无法弄清楚为什么我会收到此错误。有人可以帮帮我吗?
我查看了其他各种 SO 问题,它们似乎已由 strong_params 解决,但我的已声明 strong_params 并且无法正常工作。
【问题讨论】:
-
我可以帮助你,但我需要一些额外的信息:首先,我在任何地方都看不到对
recipient_params的调用,其目的是过滤来自表单的内容。因为它没有被称为它,所以不能犯那个错误......其次,你能在日志中看到ActiveModel::ForbiddenAttributesError的一小块吗?也许我们可以找到一些有助于理解它的来源 -
你没有在任何地方使用
recipient_params,仅仅声明它是不够的 -
感谢您的cmets,我已经用日志更新了问题。 @Iceman,我将如何使用
recipient_params来更新多个?
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 strong-parameters