In my RoR application, I have an update_multiple
method that updates multiple records with the user's inputs. However, for some reason I get the error ActiveModel::ForbiddenAttributesError
despite using strong params. Can someone please help me fix this?
The update_multiple
method in Recipients_Controller is as follows:
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
This method takes the user input from this form:
<%= 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 %>
I really cannot figure out why I am getting this error. Can someone please help me?
I have looked at various other SO questions and they seem to have been solved by strong_params
, but mine already has strong_params
declared and isn't working.
Aucun commentaire:
Enregistrer un commentaire