Ask A Question

Notifications

You’re not receiving notifications from this thread.

Nested attributes permit

Sebastian Edler asked in Rails

Hey out there,

i am sorry to bother you with my stupid question, but it is important for me, and unfortunately I am not able to find an answer myself.

I have tried to conduct the documentation, but I am unable to find a helpful (to me) anwer.
https://guides.rubyonrails.org/action_controller_overview.html#strong-parameters --> 4.5.2 "nested parameters"

I try to safe a data for two different models, in one go. I use the accepts_nested_attributes_for in the parrent class, and I am able to get the parameters:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"-cut-", "customer"=>
{"name"=>"2222", "city"=>"2222222", "mails"=>{"mailaddress"=>"22222222"}}, "commit"=>"Save Customer"}

But I get en error message:
Unpermitted parameter: :mails

I tried to whitelist the parameters ... but without success:

    private
    def customer_params
      params.require(:customer).permit(
        :name, :city,
        :mails_attributes => [:mailaddress])
    end

What have I done wrong?

Thanks for taking a look!

Reply

Hey Sebastian,

To properly use accepts_nested_attributes_for, your form needs to submit the mails_attributes key. Yours is called just mails, so you'll need to check into what's going on that it didn't generate the correct name there.

Your form should have a <%= f.fields_for :mails do |m| %> line in it which triggers it to generate those fields with the correct mails_attributes name.

Reply

Hey!

thank you for your reply.

Unfortunately that seems to be xactly what I have as my code to build the Form
Here is my form.html.erb. I cut out the unnessesary fields, as well as the error handling:

<%= form_with scope: :customer, url: customers_path, local: true  do |form|%>

    <%= form.label :name %>
    <%= form.text_field :name%>

  <p>
    <%= form.fields_for :mails do |mf| %>
      <%= mf.label :mailaddress %>
      <%= mf.text_field :mailaddress %>
    <% end %>
  </p>

  <p>
    <%= form.submit %>
  </p>

<% end %>
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,265+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.