views:

101

answers:

3

I have a photos model and a gallery model. The gallery model(I used nifty scaffold) just has one field, a gallery name. In my photo edit form I want to link each photo to a gallery, eg. I create 2 separate galleries 2009 and 2010, I want to have a dropdown list on each photo edit page with the list of galleries, so each photo can be placed in a gallery. I've been using this and this as starting points but I'm stuck, and can't get the galleries to appear in my photo form.

 class Gallery < ActiveRecord::Base
  has_many :photos
  attr_accessible :name
 end

class Photo < ActiveRecord::Base
  belongs_to :gallery
  accepts_nested_attributes_for :gallery, :allow_destroy => true

views/photos/_form.html.erb

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

  <p>
    <%= photo_form.label :title %><br />
    <%= photo_form.text_field :title %>
  </p>

<p>
    <% photo_form.fields_for :gallery do |gal_form| %>

        <%= gal_form.label :name %> 
        <%= gal_form.collection_select :gallery_id, Gallery.all, :id, :name %>
  </p>

      <% end %>

  <p>
    <%= submit_tag %>
  </p>

<% end %>

Currently there is no dropdown list on the photo form page, though I'm not receiving any errors and there is no mention of it either in the page source. I'd appreciate any help or to be pointed in the right direction...

+1  A: 

If your photo belongs to the gallery then isn't the gallery_id on the photo? So the gallery_id would be a member of the photo_form not the the gal_form.

<%= photo_form.collection_select :gallery_id, Gallery.all, :id, :name %> 

UPDATE:

Here is how I would see your view:

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

  <p>
    <%= photo_form.label :title %><br />
    <%= photo_form.text_field :title %><br />
    <%= photo_form.collection_select :gallery_id, Gallery.all, :id, :name %>
  </p>
  <p>
    <%= submit_tag %>
  </p>

<% end %>

Your model:

class Gallery < ActiveRecord::Base
  has_many :photos
end

class Photo < ActiveRecord::Base
  belongs_to :gallery
end
Geoff Lanotte
with that I get an error.. undefined method `gallery_id'
raphael_turtle
can you show add your database schema? Because if the photo is going to be in one gallery, then your models appear correct. But you should have a gallery_id column in your photo table. Also, just to confirm, you have the `@photo = Photo.find(id of photo)`?
Geoff Lanotte
I didn't have a galleries_id column but I added one though it makes no difference, and the show method for photos is correct, @photo = Photo.find(params[:id])
raphael_turtle
Geoff is correct and your way of using accepts_nested_attributes_for is wrong, it's the other way around. galery_id is the foreign key for photos table, if your database structure is right you can use the way Geoff suggested.check this - http://guides.rubyonrails.org/association_basics.html#the-has-many-association
Vamsi
could you explain what is wrong with accepts_nested_attributes_for?also I created a migration and added a galleries_id column to the photos table, is that correct?
raphael_turtle
I added how I would see your form/models. I think the error you were getting the second time around was because of the attr_accessible not allowing access to the gallery id. Definitely check out the guide that Vamsi recommended.
Geoff Lanotte
thanks Geoff, I appreciate the help!
raphael_turtle
A: 

It sounds like you don't need a nested form. I would drop accepts_nested_attributes_for entirely.

The work flow should be: 1) select a gallery 2) Upload and describe photo

You can have a separate controller/view for managing galleries.

Once you've done that and verified your schema, the example above should work (collection select on galleries tied directly to Photo)

Winfield
I don't understand what you mean, that I shouldn't have a gallery model?The workflow is correct, photos are primary, galleries are secondary, not all photos will be in a gallery.
raphael_turtle
A photo may or may not be in a gallery, and only one gallery, right? So you just need to have a collection_select on photo for gallery. You can manage creation/editing of galleries elsewhere.
Winfield
A: 

@Raphael:

The column name should be gallery_id(model name + id), after adding it

Try this below line in your ruby console

photos = Photo.find_all_by_gallery_id(Gallery.first)

This should give you list of all photos in the first gallery.

Vamsi
this just returns [], and shouldn't return anything as I haven't added any photos to any gallery because that what I'm trying to get working!
raphael_turtle
if it returns an empty array then this should work<%= photo_form.collection_select :gallery_id, Gallery.all, :id, :name %>
Vamsi
Glad to see you got it working.
Vamsi