Rails 3 - 构建表单以更新记录

时间:2010-11-01 18:31:32

标签: ruby-on-rails ruby-on-rails-3

我有以下两种模式:

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

class Photo < ActiveRecord::Base
    belongs_to :photo_album

当用户正在查看照片时,网址如下:“/ photo_albums / 40 / photos?page = 6”

在这个照片视图页面(在相册内)我想给用户提供更新图像的选项,所以我使用以下内容:

<% 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">

我认为哪个是对的?问题是,当我点击更新时,我收到以下错误:

No route matches "/photo_albums/40/photos"

这是我的路线,看起来不错,不是吗?

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"}

思考?感谢

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

1 个答案:

答案 0 :(得分:1)

尝试将表单更改为:

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

其中@photo_album是您的photo_album对象,或

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

基本相同。要使其正常工作,您应该在route.rb中包含以下内容:

resources :photo_albums do
  resources :photos
end