2010-11-01 41 views
1

我有以下兩種模式:的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 /照片頁? = 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"} 

想法?感謝

UPDATEW¯¯配置路由文件:

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 
+0

沒關係,以後我需要這個「<%的form_for [:photo_album,@ photos.first]:URL => photo_album_photo_path,:HTML = > {:multipart => true} do | f |%>「試過,但是它出現錯誤」沒有路線匹配{:action =>「destroy」,:controller =>「photos」}「有趣的是我有一個缺點我正在嘗試更新不會銷燬。 – TheExit 2010-11-01 18:35:28

+0

使用我的路線更新 – TheExit 2010-11-01 18:40:13

+0

在更新:url arg後,

標記的外觀如何? – Samo 2010-11-01 18:51:23

回答

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 
+0

哇,工作。所以我應該使用@photo_album,而不是:photo_album,有什麼區別?爲什麼這使它工作? – TheExit 2010-11-01 21:32:02

+0

此外,paper_clip刪除更新替換的舊照片。有沒有辦法保留照片的版本歷史記錄?我想回形針不要刪除以前的照片。想法? – TheExit 2010-11-01 21:32:31

相關問題