1

I have some rails nested forms

    = form_for @model do |f|
    ...
      = f.fields_for :user_partnership do |builder|
        - user_partners.each do |index|
          = builder.hidden_field :user_partner_id
          = content_tag(:li,index.name)

In my model I have set has_many :user_partner, through: :users_partnership.

So now I have two problems : the first, I have no input inside the fields_for process. So, following some SO posts, I had to add this

@model = model.new
@user_partners = User_Partner.all
@user_partners.count.times{@model.users_partnership.build} 

but this really seam awkward. The secound question is how I get the enumerator of the fields_for block so I can set correctly index.name ?

Edit, here's my models

class model
  has_many :user_partners, through: :users_partnerships
  accepts_nested_attributes_for :users_partnerships
end

class User_Partner
  has_many :models, through: :users_partnerships
end

class Users_Partnership
  belongs_to :model
  belongs_to :user_partner
end
3
  • 1
    It feels like there are so many missing connections, can you post your models in question and explain a little more what you are trying to do. Commented Aug 21, 2013 at 22:37
  • Are you sure your model class is infact all lower-case, and the other two classes have underscore in them? Also, don't they inherit from ActiveRecord::Base? More importantly what I want another clarification is what is users in your view, where does it come from? Commented Aug 21, 2013 at 22:56
  • @vinodadhikary sorry i have edited my post, it is in fact user_partners and not users. Btw, I tried ` = builder.options[:child_index]` to see if the index was showing, but nothong was displayed Commented Aug 21, 2013 at 23:27

2 Answers 2

1

First, fields_for creates a scope around model and not tag inside. So, as far as I know you have, you should build it from controller the appropriate models you want to loop through.

Second, since you indicated in some comment that = builder.options[:child_index] didn't work and that there are as many user_partners as user_partnerships, you can try this :

= @model.user_partnerships.each.with_index do |user_partnership,index|
    = f.fields_for :user_partnerships, user_partnership do |builder|
      %ul
        = builder.hidden_field :user_id
        = content_tag(:li, :id => "#{users[index].name})"

Btw, the iterator for fields_for seems to have been a long standing question, and it was expected that would come with rails 3.2 some method fields_for_with_index. I found this post interesting, but in my rails 3.2.13, I didn't find any method of this kind. So perhaps, we can rely on *.options[:child_index] but I never tested it !

Sign up to request clarification or add additional context in comments.

Comments

0

If I am understanding your question correctly, you need fields for user_partners for each instance of users.

In your controller you need

@instance_variable = @model.user_partnership.build

Then you should loop over the users and build a form for each instance.

= form_for @model do |f|
...
 - users.each do |index|
  = f.fields_for :user_partnership do |builder|
      = builder.hidden_field :user_partner_id
      = content_tag(:li,index.name)

Like others have pointed out your classes should inherit from activerecord and you to check the case and pluralization. Give the guide a read. http://edgeguides.rubyonrails.org/active_record_basics.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.