1

Rails newbie here! I'm attempting to create a form that allows User to add multiple Forecasts on one page. (Ideally, you would be able to enter an entire week of Forecasts at one time.) In the app, the two models are nested. (You can only get to Forecasts through a User.)

I modified the Forecast form and controller based on this response (also watched Railscast #196) ad while the form will now build any # of Forecast objects I need, it only saves the last one. What am I missing? Or what am I misunderstanding? Any help is really appreciated! I'm using Rails 3.2.6:

class Forecast < ActiveRecord::Base
attr_accessible :castdate, :level, :user_id

belongs_to :user



class User < ActiveRecord::Base
attr_accessible :defaultcast, :email, :password, :username, :name, :forecast_attributes

has_many :forecasts

accepts_nested_attributes_for :forecasts

In Forecast controller:

def new
@user = User.find(params[:user_id])
@forecast = @user.forecasts.build
@forecasts = Array.new(3) { @user.forecasts.build }

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @forecast }
end
end

Forecast _form:

<%= form_for(:forecasts, :url => user_forecasts_path(@user)) do |f| %>

(I attempted to use the suggested form_for([@user, :forecasts]) from the linked response above but received this error: undefined method `model_name' for Symbol:Class )

<% @forecasts.each do |forecast| %>
<%= fields_for forecast do |r| %>

<div class="field">
    <%= r.label "Date" %><br />
    <%= r.date_select :castdate, :start_year => Time.now.year,
:use_two_digit_numbers => true %>
</div>
<div class="field">
    <%= r.label "Grump Level" %><br />
    <%= r.number_field :level, :min => 0, :max => 10 %>
</div>    
<% end %>
  <% end %>
<div class="actions">
    <%= f.submit %>
</div>

<% end %>
Community
  • 1
  • 1
delovely86
  • 53
  • 3

1 Answers1

1

Normally this behavior happens when all form elements have the same name in your form. Then they all get submitted, but the last one overwrites all the other ones (actually each subsequent one overrides the previous one if you know what I mean). So the last one wins. To check out if this is you case you can either check the Rails log and look at the POST request and there at the parameters. Do they all have the same name? Or you can inspect your form and make sure that they are all named uniquely.

What you want in your HTML is something like this for a form element:

name="user[forecase_attributes][][level]"

and not

name="user[forecast_attribues][level]"

I like to use the Cocoon Gem in these cases, then in your case you can add the forecasts dynamically into your pages (and don't have to prepare three). The gem takes care of all the parameter naming, deleting etc. for you. Hope it helps...

Christoph Eicke
  • 1,134
  • 7
  • 15
  • Ah, yes, you are correct. Each element has the same name. I thought though, based on Railscast #196, that building 3 separate ones eliminated that problem... but I guess not... Is there any way besides a gem to fix this? (I'm not opposed, I just like taking the hard way for learning purposes.) – delovely86 Aug 05 '13 at 17:36
  • Please have a look at this: [Rails Guides Form Helpers - building complex forms](http://guides.rubyonrails.org/form_helpers.html#building-complex-forms). The key here is that you don't have a build your forecasts in the controller, but rather have the form take care of your `has_many` association: `<%= form_for @user do |f| %> ... <%= f.fields_for :forecasts do |r| %> ... <%= r.number_field :level, :min =>, :max => 10 %>` etc. – Christoph Eicke Aug 06 '13 at 07:42