0

I'll try and explain this as much and as easily as possible.

I have a Rails form, and 3 models. Models: DemoModule, SalesDemo, and SalesDemoModule

What I want to do in my view/form is create a new SalesDemo, but a SalesDemo has many SalesDemoModules.

In the controller I have:

@sales_demo = SalesDemo.new
@demo_modules = DemoModule.find(:all, :conditions => ['active = true'])

How can I, in my view, have a text field row for each DemoModule, which I can pass back to the controller action, to save into SalesDemoModule?

2 Answers 2

2

You can specify that the SalesDemo accepts_nested_attributes_for SalesDemoModule, which then allows you to created a nested form (i.e. within a form_for a SalesDemo, you can have fields_for SalesDemoModule). Here's a simple example.

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

2 Comments

Thanks for the info, however this only seems to work if I have 1 SalesDemoModule? What I have is 10 DemoModules, and in the view, I want to list them with a text field. In the test field for each module you enter a letter, if the text field has a value in it, I want to create a new SalesDemoModule record, with the sales_demo_id and demo_module_id so I can link them up later on?
Check out this example for more details, including examples of 1 to many, including creating records:ryandaigle.com/articles/2009/2/1/…
1

Simply put:

<%= form_for @sales_demo do |sales_demo_form| %>
  <%= sales_demo_form.text_field "some_sales_demo_property" %>
  <%= sales_demo_form.fields_for @demo_modules do |modules| %>
    <%= modules.text_field "some_module_text_field" %>
  <% end %>
<% end %>

In the SalesDemo, you will need to have

accepts_nested_attributes_for :demo_modules

You can get some more information here.

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.