Nested Forms in Rails - Accessing Attribute in Has_Many Relation

Nested forms in rails - accessing attribute in has_many relation

You could do something like the following:

<% form_for @user, :url => { :action => "update" } do |user_form| %>
...
<% user_form.fields_for :profiles do |profiles_fields| %>
Phone Number: <%= profiles_fields.text_field :profile_mobile_number %>
<% end %>
<% end %>

But since you already have an association, then might as well use 'accepts_nested_attributes_for'

Rails form with nested attributes in has_many through relationship

Try setting the inverse_of option on the has_many associations

  has_many :admin_host_users, class_name: 'Admin::HostUser', foreign_key: 'admin_host_id', dependent: :destroy, inverse_of: 'Admin::Host'
has_many :users, through: :admin_host_users, inverse_of: 'Admin::Host'

Rails nested fields_for with has_many relationship

Try changing the form to include the id in the shipment_packages_attributes hash:

{"listing"=>{"shipment_attributes"=>{"shipment_packages_attributes"=>{"3"=>{"id" => "3", "length"=>"11"}, "4"=>{"id" => "4", "length"=>"6"}}}

Rails 4 nested attributes and has_many :through associaton in a form

When employing a has_many :through relationship, you need to pass the nested_attributes through the different models, like this:

Models

class Goal < ActiveRecord::Base
has_many :milestones, inverse_of: :goal, dependent: :destroy
accepts_nested_attributes_for :milestones, :allow_destroy => true

def self.build
goal = self.new
goal.milestones.build.milestone_programs.build_program
end
end

class Milestone < ActiveRecord::Base
belongs_to :goal, inverse_of: :milestones

has_many :milestone_programs
has_many :programs, through: :milestone_programs

accepts_nested_attributes_for :milestone_programs
end

class MilestoneProgram < ActiveRecord::Base
belongs_to :milestone
belongs_to :program

accepts_nested_attributes_for :program
end

class Program
has_many :milestone_programs
has_many :milestones, through: :milestone_programs
end

Controller

#app/controllers/goals_controller.rb
def new
@goal = Goal.build
end

def create
@goal = Goal.new(goal_params)
@goal.save
end

private

def goal_params
params.require(:goal).permit(milestones_attributes: [milestone_programs_attributes: [program_attributes:[]]])
end

Form

#app/views/goals/new.html.erb
<%= form_for @goal do |f| %>
<%= f.fields_for :milestones do |m| %>
<%= m.fields_for :milestone_programs do |mp| %>
<%= mp.fields_for :program do |p| %>
<%= p.text_field :name %>
<% end %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>

I appreciate this might not be exactly what you're looking for, but tbh I didn't read all your prose. I just gathered you needed help passing nested_attributes through a has_many :through relationship

Rails nested form with has_many :through, how to edit attributes of join model?

Figured out the answer. The trick was:

@topic.linkers.build.build_article

That builds the linkers, then builds the article for each linker. So, in the models:

topic.rb needs accepts_nested_attributes_for :linkers

linker.rb needs accepts_nested_attributes_for :article

Then in the form:

<%= form_for(@topic) do |topic_form| %>
...fields...
<%= topic_form.fields_for :linkers do |linker_form| %>
...linker fields...
<%= linker_form.fields_for :article do |article_form| %>
...article fields...

Rails 4 nested form with has_many, through and multiple select

It seems like you need to explicitly tell lab_params which attributes from lab_suppliers you need to pass like:

params.require(:lab).permit(:code, :name, lab_suppliers_attributes: [:supplier_id])

Try it and let me know.

How to save nested attributes on a has many through relationship

As it's a has_many relationship, you need to tell strong params it is an array you're sending and then the location specific attributes. Also, so it works when you edit later on, I would also permit the :id field:

params.require(:product).permit(:name, :price, stocks_attributes: [:id, location_id: []]) 

This will allow you to post an array of venues and then when you write you update action later you can also edit the venue (without the :id allowed, it will duplicate the row).



Related Topics



Leave a reply



Submit