Pass Array Hidden Field in Nested Model Rails

pass array hidden field in nested model rails

Hidden fields aren't really the issue here

Apart from @BroiStatse's answer, I can see the issue as how you handle the params on your controller


Nested Models

Sending data to a controller sends that data to the relevant models. This is normally handled with accepts_nested_attributes_for, but can be handled manually too

From your controller code, I can't see how you're dealing with your extra data, but your error is caused by the incorrect merge of the params

Instead of saving the data manually, I would use the accepts_nested_attributes_for to save the data, like this:

#app/models/project.rb
Class Project < ActiveRecord::Base
accepts_nested_attributes_for :modul1hours
end

This will pass the params to your modul1hours model, where you'll then have to capture them with the relevant attr_accessible actions


f.fields_for

In order to get accepts_nested_attributes_for working properly, you have to ensure you use the f.fields_for function correctly.

You have to first build the ActiveRecord objects in your new controller action, like this:

def new
@project = Project.new
@project.modul1hours.build
end

Your problem is that you're then cycling through the ID's of your modul1hours model, yielding the f.fields_for artificially. Rails will only output an f.fields_for if the ActiveRecord object has been built in the controller:


"30" %>

This RailsCast gives you a better idea about this

What I would do is this:

#app/controllers/projects_controller.rb
def new
@project = Project.new
@m1.map(&:id).each do |i|
@project.modul1hours.build
end
end

#app/views/projects/new.html.erb
<%= b.fields_for :modul1hours do |f| %>
<%= hidden_field_tag :id, value :id %>
<%= f.text_field :module_est_hours, :size => "30" %>
<% end %>

I'm still thinking about how I would assign the ID's to the hidden field


Update

Try this:

#app/controllers/projects_controller.rb
def new
@project = Project.new
@project.modul1hours.build
end

Replace modul1hours with whatever your projects has_many of

data not saved in nested table with hidden (array) field passed

you should add in your Esthour model:

attr_accessible :modul1hours_attributes

Param I'm trying to pass through hidden field is showing as an object?

You have this for your form:

<%= form_for item do |f| %>
<%= f.hidden_field :update_by, value: 1 %>
...
<% end %>

and item is an OrderItem. That means that everything that f's methods produce (such as that f.hidden_field) will live inside the order_item namespace. Hence the

"order_item"=>{"update_by"=>"1"}

in your controller. You can also see this in your item_params controller method:

def item_params
params.require(:order_item).permit(:quantity, :product_id, :user_id, :update_by)
end
end

The require call means that you're looking for "order_item" and the permit call means that you're looking for those things inside "order_item".

You should use your item_params method to unpack the parameters:

@item.update(:quantity => @item.quantity + item_params[:update_by].to_i)

Ruby on Rails 6: form submits an array of hashes via hidden field but gets blocked by ActionController::Parameters ..... permitted: false

You are not using your post_params method, change this:

@post = current_user.posts.build(params[:post_params])

to this:

@post = current_user.posts.build(post_params)

EDIT: note that passing the user_id as a parameter is a security risk, someone can change the hidden field value. Use the current_user id instead of receiving the id from the form.

Actually, all the hidden info is retrieved from the current_user, you don't really need to use hidden fields since if you have access to the current_user you already have access to all that (id, cart items)

EDIT 2: if you want to have an array of hashes inside the :items_bought parameter, you need to use multiple hidden fields with specific names so it's parsed as hashes by rails.

Let's say you have this structure:

[
{
id: 1,
name: 'Foo',
some_attr: 'Lorem'
},
{
id: 2,
name: 'Bar',
some_attr: 'Impsum'
}
]

If you send that as parameters you need multiple hidden fields:

hidden_field_tag 'items_bought[1][id]', 1
hidden_field_tag 'items_bought[1][name]', 'Foo'
hidden_field_tag 'items_bought[1][some_attr]', 'Lorem'
hidden_field_tag 'items_bought[2][id]', 2
hidden_field_tag 'items_bought[2][name]', 'Bar'
hidden_field_tag 'items_bought[2][some_attr]', 'Impsum'

When submitted, params[:items_bought] will be:

{
'1' => {
id: '1',
name: 'Foo',
some_attr: 'Lorem'
},
'2' => {
id: '2',
name: 'Bar',
some_attr: 'Impsum'
}
}

Note it's not the same, it's a hash of hashes and you have the ID if each hash as the key (that's the first part of the hidden field name items_bought[1] and items_bought[2], and the ids are strings, not numbers.

You could also serialize the hash as a string:

hidden_field_tag :items_bougth, my_hash.to_json

and then on the controller do

param_hash = JSON.parse(params[:items_bought]

but I woudn't recommend this, it's a sign of something done more complex than it needs to be, you need to parse the parameters, use an extra library, etc.

Note you shouldn't do any of this with complex objects like LineItem as is, you talk about an array of hashes, an array of LineItems is not an array of hashes, you have to convert the objects to hashes first for both methods to work consistently.

Rails 3: Why an empty nested form generates a hidden input field?

It'll be because the @product you're editing has a shop. Rails has inserted that in the fields_for so that when the form is submitted, it knows which shop those nested attributes are for. It's default nested attributes behaviour.

How to access a hidden field in form in the params value but it says unexpected line end

In Ruby when calling methods that take positional and keyword arguments you MUST place the keyword arguments last:

def foo(*args, **kwargs); end
foo(1, 2, 3, bar: 'baz')
foo(bar: 'baz', 1, 2, 3) # syntax error
params.require(:proposal).permit(:title, :description, :target_audience,
:details, :pitch, :difficulty, :track_id,
:main_tag_id, :tags,
comments_attributes: %i[body proposal_id person_id],
speakers_attributes: %i[person_id id])

RoR: Nested Form , Hidden Field, value

IDs are not assigned until you save the records, so there's no way the form can submit something like tsk1_id - it doesn't exist yet.

What you'll need to do is add the wiring in your controller to attach tsk2 to tsk1 and tsk3 to tsk2.

@task = Task.new(task_params)
@task.tsk1s.first.tsk2 = @task.tsk2s.first
@task.tsk2s.first.tsk3 = @task.tsk3s.first
@task.save

Part of the trouble might be that this is a strange schema. You're accessing some tskNs through: :task and others directly, which makes the wiring complicated.

You might consider removing Tsk1, Tsk2, and Tsk3 in favor of:

  1. Just a Task with an optional parent_id. Then you can build any tree of tasks and dependencies and get some functionality out of gems like acts_as_tree.
  2. Task and Subtask, with an order attribute on Subtask. Then you can assign Subtasks to Tasks and specify the order they need to be finished in.

Rails' form helpers will handle these cases much better than your current version.



Related Topics



Leave a reply



Submit