TheExit
TheExit

Reputation: 5320

Rails 3 - Building a Form to Update a Record

I have the following two models:

class PhotoAlbum < ActiveRecord::Base
    belongs_to :project
    has_many :photos, :dependent => :destroy

class Photo < ActiveRecord::Base
    belongs_to :photo_album

When the user is viewing a photo, the URL is like follows: "/photo_albums/40/photos?page=6"

On this photo view page (inside an album) I want to give the user the option to update the image, so I'm using the following:

<% form_for [:photo, @photos.first], :url => photo_album_photos_path, :html => { :multipart => true } do |f| %>


<form method="post" id="edit_photo_124" enctype="multipart/form-data" class="edit_photo" action="/photo_albums/40/photos" accept-charset="UTF-8">

Which I think is right? Problem is, when I click update, I get the following error:

No route matches "/photo_albums/40/photos"

Here are my routes, which look good, no?

photo_album_photo PUT    /photo_albums/:photo_album_id/photos/:id(.:format)       {:action=>"update", :controller=>"photos"}
photo PUT    /photos/:id(.:format)                                    {:action=>"update", :controller=>"photos"}

Thoughts? thanks

UPDATE w config route file:

resources :photos do
    resources :comments, :only => [:create, :update,:destroy], :constraint => {:context_type => "photos"}
    collection do
              post 'upload'
      get 'search'
    end
end


  resources :photo_albums do
        resources :comments, :only => [:create, :update,:destroy], :constraint => {:context_type => "photo_albums"}
      resources :photos
      collection do
          get 'search'
      end
    end

Upvotes: 1

Views: 1174

Answers (1)

Yannis
Yannis

Reputation: 5426

Try to change your form to something like:

<% form_for [@photo_album, @photos.first], :html => { :multipart => true } do |f| %>

where @photo_album is your photo_album object, or

<% form_for @photos.first, :url => photo_album_photo_path(@photo_album, @photo), :html => { :multipart => true } do |f| %>

which is basically the same. To have this working, you should have the following in your route.rb:

resources :photo_albums do
  resources :photos
end

Upvotes: 1

Related Questions