1

I have a many-to-many relationship with a link box, and I want to pull those models together into one form so I can update from the same page. I'm really struggling with getting the check_box to even show all the elements of my array - I've scoured the net and been working on this literally all day, and I'm finding it difficult to apply the information I'm reading to my problem. I'm also extremely new to RoR and I've been following a bit of an outdated video tutorial (pre 2.0) so apologies for my code. So far, I've got it to output only one key pair in the array (the last one) - although outside the form, the code used in the tutorial works exactly how it should. Thats of little use though! Host is the model for which the main form is for, and Billing is the outside model that I'm trying to add to the form.

This is the code that works outside of the form from the tutorial:

<% for billing in @billings -%>
<%= check_box_tag('billing_title[]', billing.id, @host.billings.collect
{|obj| obj.id}.include?(billing.id))%> <%= billing.title %><br />
<% end -%>

I just need to know how to make it work inside the form. This is the aforementioned code that only retrieves the last array keypair after looping through them:

<% f.fields_for :billings do |obj| %><br />
<%= check_box_tag('billing_title[]', billing.id, @billings.collect
{|obj| obj.id}.include?(billing.id))%> <%= billing.title %><br />
<% end %>

The debug(@billings) :

--- 
- !ruby/object:Billing 
attributes: 
title: Every Month
id: "1"
attributes_cache: {}

- !ruby/object:Billing 
attributes: 
title: 12 Months
id: "2"
attributes_cache: {}

- !ruby/object:Billing 
attributes: 
title: 6 Months
id: "5"
attributes_cache: {}

Any help really appreciated.

1
  • pastebin.com/Q4znSMyA Heres a pastebin if it helps Commented Mar 30, 2010 at 0:29

3 Answers 3

2

Craig, it sounds like what you are looking for is accepts_nested_attributes_for which is a much better way of handling nested models in a form.

Rather than just steal all of Ryan's work and repost it here, I'll just give you a link to his screencast:

http://railscasts.com/episodes/196-nested-model-form-part-1

This is based off his complex forms series.

You should be able to taylor this tutorial to what you are trying to do, but if not I'd be happy to help.

EDIT:

Alright, after looking at your code, there are a few things causing you problems. :)

First of all, with associations, you don't need to pull an extra collection for billings, i.e.:

@host = Host.find(params[:id])
@voips = Voip.find(:all)
@custsupps = Custsupp.find(:all)
@payments = Payment.find(:all)
@billings = Billing.find(:all) # <-- This is not needed and causing your problems

The association setup in the model does this all for you. This is part of the magic of rails. :D

Now, it is important to note that when using associations you need to make sure that the objects are actually associated. In other words, if you have 3 Billings objects in your database and they are not associated with your Host object, they won't show up in the form.

If you are trying to associate a billing TO a Host using a checkbox you are going to want to take a different approach, because your form with only display Billings already associated with your Host.

If your just trying to edit or modify existing Billings that are associated to the Host, where the checkbox represents a 'paid' attribute (a boolean) for instance, then this approach is fine and your form code would look something like this:

<% f.fields_for :billings do |b| %><br />
  <%= b.check_box :paid %> <%= b.title %>
<% end %>

So perhaps clarify what your trying to accomplish from a functionality standpoint and we can find a better solution.

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

14 Comments

Thanks for replying dustmoo! I've actually seen that tutorial and already have my accepts_nested_attributes_for inplace in my Host model, at the moment I haven't even got to the stage of submitting my form yet - I can send the array to the form page, but I can't build my check_boxes using the field_for function. Its a right headache! but I'd really appreciate your help in working this out, I'm almost at the point of scrapping RoR and going back to php as I've been working on this for too long - but really I want to sort this out - I don't like quitting :) what info can I supply you?
You know, I must have been asleep at the wheel yesterday, I just looked at your code again and see the problems. :D I'll post a fix soon.
Thats an interesting point dustmoo - so I could put it in a partial and it would work just the same? Could I put the fields_for in a loop of its own to create the list of check_boxes? Thanks loads for the help!
I'm glad its something obvious in a way haha, means it shouldnt (in theory) be too difficult to fix? This is the only page in the whole backend that needs this function, so once its over with I can carry on with the creating :) your helps really appreciated thanks.
lol.. well not too obvious, I think it is just a disconnect between what you are trying to accomplish and Rails conventions. :) Read my edits and we'll go from there. :)
|
0

i'm not sure about my answer, but i'll try...

<% f.fields_for :billings, @billings do |obj| %>
   # ...
<% end %>

2 Comments

Thanks for the attempt :) though this gives me the exact same output as the method I tried - I've actually tried quite a few variations of it over the course of the day (yep, I've been working on this one problem for around 10 hours today argh!) cheers again :)
Yeah, this updated code gives me the same as the one I posted - one checkbox (the last in the array - 6 months') - mini-screengrab: i39.tinypic.com/xcvq6q.jpg
0

Looks to me like it's a problem with the for-loops...

In your example code, you are looping through the @billings (an array of billings objects passed from your controller), and calling the current one billing.

In your form code, you are looping through the :billings (symbol referencing the billings ActiveRecords) and calling the current one obj. Then, confusingly, your code re-uses the variable name obj again in the @billings.collect function... but this should have local private scope and not effect your main loop, but it is hard to interpret on sight.

Anyway, since the stuff inside your form loop doesn't reference obj, it is using the variable billing--which does not change when you loop through, but keeps the last value it had: the last item from the previous loop! This explains why you only see the last tag. (Also, the debug(@billings) function does its own looping through the records of sorts--it is not inside the fields_for do loop.)

I would start with changing the first line of your form code to re-use the variable name billing again, like this:

<% f.fields_for :billings do |billing| %><br />

Ruby's block syntax can be pretty confusing at first--or, heck, anytime you're reading code you didn't write yourself--but when you break it all down to the basic components, it's usually just looping through a set of items and performing some function on each one. Also watch out for the confusion between similar-looking names: @billing, @billings, :billings and billings are all different things to Ruby and Rails. Good luck!

1 Comment

Thanks for this :) I've changed the line you mentioned, and now I'm getting an error: undefined method `title' for #<ActionView::Helpers::FormBuilder:0x48a0c08> This must relate to the billing.title which is used to label my form: <%= billing.title %> Although if I remove the label completely, I then (again) only get a single (this time unlabelled) check box for the last item in the array. This is all very confusing, but I really appreciate your help. I mean the debug shows the key pairs are there in the billings array - I just cant seem to loop through the array! Thanks again

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.