views:

35

answers:

3

(I'm just learning rails so....) I have a photo model and a gallery model, habtm associations and a join table. I'm making a photo gallery. The gallery page has a title field and description field. User creates gallery title, then goes to the photo page and checkboxes each image they want in that gallery.

I get the error "undefined method `to_i' for ["1", {"title"=>"1"}]:Array" when trying to save/update a photo with a gallery title(with the checkbox)

<% form_for @photo, :html => {:multipart => true } do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :description %>
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :image %>
    <%= f.file_field :image %>
  </p>


<% for gallery in @photo.galleries %>
<% fields_for "painting[gallery_attributes][]", gallery do |g| %>
<div>
  <%= g.check_box :title %>
  <%= gallery.title %>
</div>
<% end %>
<% end %>
<p><%= submit_tag 'Update' %></p>
<% end %>

How much of this is horribly wrong? Can someone point me in the right direction?, I can't find any tutorials relating to this for 2.3 and above.

+1  A: 

For this complicated task of having multiple models updated with a single form, i would recommend the three part series in railscasts:

http://railscasts.com/episodes/73-complex-forms-part-1

http://railscasts.com/episodes/74-complex-forms-part-2

http://railscasts.com/episodes/74-complex-forms-part-3

Though please if another forum member knows of a better/more up-to-date tutorial let me know, i did this back in the day and it was a pain.

ThinkBohemian
Ryan did an update on these railscasts:http://railscasts.com/episodes/196-nested-model-form-part-1http://railscasts.com/episodes/197-nested-model-form-part-2
sosborn
A: 

I thought this was a good example of nested forms http://github.com/alloy/complex-form-examples/

Anna
A: 

I've also run into the "undefined method 'to_i'" recently with checkboxes. Are you using searchlogic in this project? If so, try using version 2.4.14. It appears there was an issue introduced at version 2.4.15 and not yet fixed as of 2.4.19. If you install and specify 2.4.14, your problem should be fixed. Again, this all assumes that you are using searchlogic.

Tony