Unpermitted Parameters for Dynamic Forms in Rails 4

Unpermitted parameters for Dynamic Forms in Rails 4

If you want to return a nested hash as a parameter you have to name the keys in the array in permit.

class ProductsController < ApplicationController
def new
@product = Product.new(product_type_id: params[:product_type_id])
end
def product_params
params.require(:product).permit(:name, :price, :product_type_id, {:properties => [:foo, :bar, :id]})
end

If you are generating the keys dynamically and can't code them into the permit statement then you need to use this style:

def product_params
params.require(:product).permit(:name, :price, :product_type_id).tap do |whitelisted|
whitelisted[:properties] = params[:product][:properties]
end
end

It's not the most friendly code for a new user, I just finished the 3 course rails certificate at UW and they never even covered .tap.

This is not my work, I'm still just understanding the deeper parts of .permit like this. This is the blog entry I used: Strong Parameters by Example

Unpermitted parameter for array with dynamic keys

It's not really clear what service_rates is for you. Is it the name of an association ? Or just an array of strings ?

To allow array of strings : :array => [],
To allow nested params for association : association_attributes: [:id, :_destroy, ...]

params.require(:object).permit(
:something,
:something_else,
....
# For an array (of strings) : like this (AFTER every other "normal" fields)
:service_rates => [],
# For nested params : After standard fields + array fields
service_rates_attributes: [
:id,
...
]
)

As I explained in the comments, the order matters. Your whitelisted array must appear AFTER every classic fields

EDIT

Your form should use f.fields_for for nested attributes

<%= form_for @project do |f| %>
<%= f.fields_for :service_rates do |sr| %>
<tr>
<td>
<%= sr.text_field(:value, class: 'uk-width-1-1', placeholder: 'Stundensatz' %>
</td>
</tr>
<% end %>
<% end %>

Unpermitted parameters for nested form many-to-many relationship Rails 4

Change:

<%= f.fields_for [@post, Tag.new] do |tag_form| %>

to

<%= f.fields_for(:tags, Tag.new) do |tag_form| %>

Rails 4 Unpermitted Parameters for Array

Try this

params.require(:post).permit(:name, :email, :categories => [])

(Disregard my comment, I don't think that matters)



Related Topics



Leave a reply



Submit