1

I have what seems like a simple query. I need to create a view that will accept multiple records based on a single model. In my case the model is Project, which has 1 foreign key (person) and 2 fields time, role. I need to create a view (form) to insert 5 roles.

<%= form_for(@project) do |f| %>
 <% 5.times do |index|%>
  <div class="field">
    <%= f.label :position %><br />
    <%= f.text_field "fields[#{index}][stime]" %>
  </div>

 <% end %>
 <div class="actions">
     <%= f.submit %>
 </div>  
<% end %>

I get an error message: undefined method `fields[0][stime]'

I do not think the railscasts for nested models is what I need.

How would I go about creating this?

EDIT: The Project model code is below:

class Project < ActiveRecord::Base
   belongs_to :person
   attr_accessible :role, :stime
end

The Projects_Controller code for the new method is below:

def new
 @project = Project.new
end
andyd
  • 71
  • 8
  • Could you post the complete form? Sounds like a `Project` needs three attributes, `person_id`, `time` and `role`, yet there is only a single input for `stime`? Not clear what's going on here. Posting your `Project` model would also be helpful. – Substantial Dec 01 '12 at 10:20
  • I have added the Project model. I created a view to display only one field as of now. I suppose that should not be an issue. – andyd Dec 01 '12 at 10:45
  • Please post your controller code. – Pavel Strakhov Dec 01 '12 at 10:50
  • Your model shows `role` as a single attribute of `Project`, yet you call for inserting five roles. How? What is your intended result? – Substantial Dec 01 '12 at 10:56
  • @Riateche I have added the controller code, it does not do anything specific. Do I need to specify that I need 5 roles in the controller? – andyd Dec 01 '12 at 11:00
  • @gg_s - While defining a project, I also want to create 5 roles within that project. Rather than entering them in 5 forms, I would like to capture them at once. – andyd Dec 01 '12 at 11:02
  • What type is `role`? (string? array?) Are you using serialization? – Substantial Dec 01 '12 at 11:18
  • This task is not that easy. See http://stackoverflow.com/questions/972857/multiple-objects-in-a-rails-form – Pavel Strakhov Dec 01 '12 at 11:19
  • @gg_s - It's a string. Am not using serialization. – andyd Dec 01 '12 at 11:20
  • @Riateche - Thanks. One option would be 2 use nested models and re-create my current Project model in order to include something like Project_lines? – andyd Dec 01 '12 at 11:25
  • @andyd You basically want several strings to be concatenated into one string and saved to the database? You will not end up with five discreet roles, but one large combined role which cannot be (easily) converted back into discreet roles. Is this correct? – Substantial Dec 01 '12 at 11:44

1 Answers1

1

I see you're planning to make some 1-to-many relationship (Product has_many :roles).

Here's some advices.

First, take a look at the accepts_nested_attributes_for method. You need to add it to your model to be able to perform mass-create.

Second, fields_for is what you need to design nested forms.

I'll give you some example of mass-creating for a simple Product has_many :line_items case:

<%= form_for @product do |f| %>
    <%= f.fields_for :line_items, [LineItem.new]*5 do |li_fields| %>
      <%= li_fields.text_field :quantity %>
      <%= li_fields.text_field :price %>
      <br>
    <% end %>
  <%= f.submit "Create line items" %>
<% end %>

All you need is to write in you controller something like:

@product.update_attributes params[:product]

and 5 line_items will be created at once.

Don't forget to white-list association_attributes (see params in your logs to see it). But I think if you get the mass-assignment error you'll do it anyway :)

I hope it helps.

Community
  • 1
  • 1
jdoe
  • 15,665
  • 2
  • 46
  • 48
  • I thought this was the case also, but there appears to be no association, nor a `Role` class, nor a `roles` table. `role` is simply a string attribute of `Project`, by design I suppose. Normalizing `role` makes sense here, but I wonder if OP did it his way intentionally. – Substantial Dec 01 '12 at 11:53
  • Thanks jdoe. I think, instead of roles, we can replace it with persons. So a Project has many persons. So nested forms is the way to go? – andyd Dec 01 '12 at 12:02
  • @andyd Yes, nested forms can handle this. – jdoe Dec 01 '12 at 12:36
  • 1
    I think that has to be the way to go, else it might just get convoluted. I have used the following tutorial: [link](http://railscasts.com/episodes/196-nested-model-form-part-1) from Railscasts.. – andyd Dec 01 '12 at 12:57