0

There are rfq, quote and test_item in our rails 3.1.0 app. RFQ has many quotes and test items. Quote belongs to a rfq and has many test items. Test item has many rfqs and quotes.

When creating a quote, the test items in the rfq are passed into the quote new form directly. Here is the code in new in quotes controller:

  @quote = @rfq.quotes.new()
  @quote.test_items << @rfq.test_items

Here is the view code in quote new form for displaying the test items passed from the rfq:

<%= simple_form_for([@rfq, @quote]) do |f| %>    
....        
  <% @quote.test_items.each do |t| %>   
    <p><%= f.association :test_items, :label => false %><%= link_to_remove_fields "remove", f %></p> 
  <% end %> 
....  
<% end %> 

The view code above can display the test item passed from the rfq and maintain the association (quote has many test items) which is what the app needs. However it also displays a selection box for test item which is not needed here(can't change the test item in quote other than delete. Also t in the loop hasn't been useful). What we need is only to display the name of the test items, maintain the association (quote has many test items) and allow to delete test item (done by link_to_remove_fields).

Is there a clean way to accomplish this? Thanks so much.

1 Answer 1

1

Take a look at the nested attributes mechanism in Rails. It requires both a model declaration, accepts_nested_attributes_for and is supported by the fields_for form helpers that are also compatible with simple_form's simple_fields_for.

I think your error is that you loop over the test_items explicitly and call f.assocation which tells the form that you'll want to choose a different test_item. I think what you're trying to do is to let the user remove test_items with a check box. You'll need to use fields_for to implicitly loop over the list of test_items and use nested_attributes to permit deletion.

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

1 Comment

Tried to use the simple_fields_for before association, somehow could not get it right.

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.