Rails 4 Unpermitted Parameters for Array

Rails 4 Unpermitted Parameters for Array

Try this

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

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

Rails 4 Unpermitted parameters: products

You need to do it in a Rails way...

In models/order.rb

class Order < ActiveRecord::Base
has_many :products
accepts_nested_attributes_for :products, allow_destroy: true
end

In orders_controller.rb

def order_params
params.require(:order).permit(products_attributes: [:name, :etc])
end

In products_attributes array you can pass the product attributes you want to permit.

You need to send product_attributes like this: {“order”=>{“products_attributes"=>[{“name”=>”product 1”}, {“name”=>”product 2”}]}}

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 %>

Why does Rails keep telling me unpermitted parameter even though I have permitted it?

For array values, you have to declare them as such. For array of simple strings it can be this.

.permit(:foo, :bar, ..., attachment: [])

If you had an array of objects, you could whitelist properties of the objects

.permit(:foo, :bar, ..., attachment: [:prop1, :prop3])

It's all in the docs, by the way: Hash and Array parameters.

Rails 4 unpermitted parameters for Postgres type array

I've tested your sample code and it seems to be fine:

raw_parameters = { :image_slug => "some_slug", :category => "A", :layout_slug => ["a", "b"] }
parameters = ActionController::Parameters.new(raw_parameters)
parameters.permit(:image_slug, :category, :layout_slug => [])
# {"image_slug"=>"some_slug", "category"=>"A", "layout_slug"=>["a", "b"]}

Check your params :layout_slug format, perhaps that's the case

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 parameters for an attribute that has an array of values

You'll need to tell strong_parameters to expect an array if I recall correctly.

def node_params
params.require(:node).permit(:user_id, :family_tree_id, :name, :description, :parent_id, :parent, parents: [])
end


Related Topics



Leave a reply



Submit