Rails: Unpermitted Parameter in Rails 5

Rails: Unpermitted parameter in Rails 5

It's not the inflection of the word "criteria" that's giving you problems (although you can add a custom inflector to get the singular and plural versions you prefer if you really want).

The issue is that you have to explicitly permit the fields of nested objects.

Change your current params:

params.require(:project).permit(:name,  project_criteria: [] )

To this (for a single nested object):

params.require(:project).permit(:name,  project_criteria: [:name, :type, :benefit] )

Your case is somewhat compounded by the fact that you're dealing with multiple nested objects, so you'll have to pass a hash instead:

params.require(:project).permit(:name,  { project_criteria: [:name, :type, :benefit]} )

Unpermitted parameter: Rails 5.2

This part of your parameters "property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000", "gym"=>"1"} should has the same structure as it is in property_params.

The parameter gym has to be inside amenities_attributes.

Like this: "property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000", "amenities_attributes" => [{ "gym"=>"1" }]}.

UPD

Check this out https://guides.rubyonrails.org/form_helpers.html#nested-forms

Try to use this piece of code in the form view:

<!--Gym attribute from amenities-->
<%= form.fields_for :amenities do |amenities_form| %>
<div class="field">
<%= amenities_form.label :gym %>
<%= amenities_form.number_field :gym %>
</div>
<% end %>

Rails Unpermitted Parameter

Your whole solution is creative but extremely redundant. Instead use the collection helpers:

<%= form_with(model: @post) |f| %> 
<%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %>
<% end %>

tags_ids= is a special setter created by has_many :tags, through: :post_tags (they are created for all has_many and HABTM assocations). It takes an array of ids and will automatically create/delete join rows for you.

All you have to do in your controller is whitelist post[tag_ids] as an array:

class PostsController < ApplicationController
# ...

def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render :new
end
end

def update
if @post.update(post_params)
redirect_to @post
else
render :edit
end
end

private
# ...

def post_params
params.require(:post)
.permit(:name, :notes, tag_ids: [])
end
end

Unpermitted parameters utf8, method in update action

I think that what you are trying to achieve is something like:

if @campaign.update(campaign_params.require(:campaign).permit!)

But permit! is pretty risk. So I'd go with this:

if @campaign.update(campaign_params.require(:campaign).permit(:name, :status))

Explanation

See... When you declare a model/scope to your form, Rails will nest this parameters with the model/scope name.

So the parameters you are using inside the form are nested under the campaign key. By default, Rails will send some extra params, as you may have already noticed (authenticity_token,commit, utf8, etc...).

The unpermitted params error mean that you cannot use these params to update an entity (for security reasons) unless you explicitly declare it.

Where is my unpermitted parameter: :passenger Rails error coming from?

Your usage of fields_for is incorrect. When you look at the example in the guide you will notice that there is no need to wrap it with an .each if used for a collection.

10.2 Nested Forms

The following form allows a user to create a Person and its
associated addresses.

<%= form_with model: @person do |form| %>
Addresses:
<ul>
<%= form.fields_for :addresses do |addresses_form| %>
<li>
<%= addresses_form.label :kind %>
<%= addresses_form.text_field :kind %>

<%= addresses_form.label :street %>
<%= addresses_form.text_field :street %>
...
</li>
<% end %>
</ul>
<% end %>

When an association accepts nested attributes fields_for renders its block once for every element of the association. In particular,
if a person has no addresses it renders nothing. A common pattern is
for the controller to build one or more empty children so that at
least one set of fields is shown to the user. The example below would
result in 2 sets of address fields being rendered on the new person
form.

def new
@person = Person.new
2.times { @person.addresses.build }
end

When applying this to your code, removing the .each wrapper and changing passenger into :passengers should do the trick. You can access the index through the FormBuilder instance (form) passed to the fields_for block.

<%= form_with model: @booking, url: bookings_path do |f| %>
<%= f.hidden_field :flight_id, value: @flight.id %>

<%= f.fields_for :passengers do |form| %>
<h4>Passenger <%= form.index + 1 %></h4>

<%= form.label :name, "Full name:" %>
<%= form.text_field :name %>

<%= form.label :email, "Email:" %>
<%= form.email_field :email %>
<% end %>

<%= f.submit "Confirm details"%>
<% end %>

Unpermitted parameter: :photos_attributes

that is because you put the photos_attributes in the merge portion instead as blog_form attribute

how about this:

params.require(:blog_form).permit(:title , :content, :photos_attributes =>[:image, :id, :destroy] )

then update blogs_controller:

def create
@blog_form = BlogForm.new(blog_form_params)
@blog_form.user_id=params[:user_id]
...

then in BlogForm.rb:

def blog_builder
@user = User.find(user_id)
...


Related Topics



Leave a reply



Submit