Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Ajax fields_for issue
6 Posts
Ajax fields_for issue

Somenthing like that, I solved this problem. The code was like this:

@expense.expense_items, :locals => { :f => expense_item_form} %>

Now I changed it to:

'expense_items', :locals => { :f => expense_item_form} %>

All references to expense_item object in the partial changed for references to the form.

Sounds like that you have a partial that is iterating, and you're (probably inadvertently) using the render syntax which automatically iterates over a collection (eg. @render @some.collection@ or @render :partial => 'template', :collection => @some.collection@)

Making a correction: It is not saving duplicated items, it is rendering a full collection of items for each one of them. It means that if i have 3 list items ill see 9, 3 times each one, if i have 6 items ill see 36 and so on.

Good lead thanks. I solve that problem but now it is saving duplicate records. The test case is as follows: New list. Add an item to the list. Save the list. Edit list and add a second item. Save the list. Result: All elements in the list are dupplicated Why is this happening?

There's a fair bit of coding required to get all this stuff working nicely, the best idea is to take a look at "Eloy's example application on github":http://github.com/alloy/complex-form-examples/ He uses the :child_index and then does a JS .replace() to replace it with the date in order to get a unique, ordered index for the new elements.

I need to add multiple line items to a list and this items should be added and edited in the same new action of the list, so I am using fields_for and nested attributes to do it.

class list < ActiveRecord::Base has_many :line_items accepts_nested_attributes_for :line_items end

For each line_item added, the user must capture a category an description for the item, the price and the quantity. The sistem must calculate the total by calulating quantity * price and display.

All this using Ajax because the user must be able to finish the complete capture in the same view and the total information must be shown just as soon as the user stoped typing. The tricky thing that gots me stucked is that I need the html ids of the fields in order to perfom the calculation, to make the fields_for helper usable for this we need to specify :index => nil other wise ActiveRecord throws an error, but without that index I can not get a unique html id and thus can't calculate the total for each set of fields.

One possible solution is to remove :index => nil from fields_for helper and just remove those indexes in the model or in the controller, but I am looking for a better, cleaner solution.

¿Any ideas?

6 Posts
Login to add your message