2013-12-21 25 views
0

時候我已經用戶,相冊和照片模式設置爲以下幾點:未定義的方法 'ALBUM_URL' 刪除照片並重定向

user.rb

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    has_many :albumusers 
    has_many :albums, through: :albumusers, :foreign_key => :album_id 

end 

album.rb

class Album < ActiveRecord::Base 
    validates :name, presence: true 

    has_many :photos 

    has_many :albumusers 
    has_many :users, through: :albumusers, :foreign_key => :user_id 
end 

photo.rb

class Photo < ActiveRecord::Base 
    belongs_to :album 

    mount_uploader :image, PhotoUploader 

    before_create :default_title 

    def default_title 
     self.title ||= File.basename(image.filename, '.*').titleize if image 
    end 
end 

albumuser.rb

class Albumuser < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :album 
end 

的routes.rb

resources :users do 
    resources :albums do 
     resources :photos 
    end 
end 

photo_controller.rb

def destroy 
    @photo = Photo.find(params[:id]) 
    @photo.destroy 
    flash[:notice] = "Successfully deleted photo" 
    redirect_to @photo.album # ERROR is happening here 
    end 

當我嘗試刪除照片時,嘗試重定向回相冊顯示視圖時出現錯誤。

+0

你能從'Photo'模型添加相關代碼? – Stoic

+1

你是否在你的相冊控制器上實現了一個show view? – timpone

+0

對不起,添加了Photo模型的代碼。是的,我有一個專輯顯示視圖。 – zyang91

回答

1

的原因是在你的路線:
你的路由給你

user_album_photos GET /users/:user_id/albums/:album_id/photos(.:format)   photos#index 
         POST /users/:user_id/albums/:album_id/photos(.:format)   photos#create 
new_user_album_photo GET /users/:user_id/albums/:album_id/photos/new(.:format)  photos#new 
edit_user_album_photo GET /users/:user_id/albums/:album_id/photos/:id/edit(.:format) photos#edit 
    user_album_photo GET /users/:user_id/albums/:album_id/photos/:id(.:format)  photos#show 
         PATCH /users/:user_id/albums/:album_id/photos/:id(.:format)  photos#update 
         PUT /users/:user_id/albums/:album_id/photos/:id(.:format)  photos#update 
         DELETE /users/:user_id/albums/:album_id/photos/:id(.:format)  photos#destroy 
      user_albums GET /users/:user_id/albums(.:format)       albums#index 
         POST /users/:user_id/albums(.:format)       albums#create 
     new_user_album GET /users/:user_id/albums/new(.:format)      albums#new 
     edit_user_album GET /users/:user_id/albums/:id/edit(.:format)     albums#edit 
      user_album GET /users/:user_id/albums/:id(.:format)      albums#show 
         PATCH /users/:user_id/albums/:id(.:format)      albums#update 
         PUT /users/:user_id/albums/:id(.:format)      albums#update 
         DELETE /users/:user_id/albums/:id(.:format)      albums#destroy 
       users GET /users(.:format)           users#index 
         POST /users(.:format)           users#create 
      new_user GET /users/new(.:format)          users#new 
      edit_user GET /users/:id/edit(.:format)         users#edit 
       user GET /users/:id(.:format)          users#show 
         PATCH /users/:id(.:format)          users#update 
         PUT /users/:id(.:format)          users#update 
         DELETE /users/:id(.:format)          users#destroy 

正如你所看到的,沒有所謂的album只有一個叫user_album路線。

您可以畫到相冊直接路由,這樣你
的routes.rb:

resources :users do 
    resources :albums do 
     resources :photos 
    end 
end 
resources :albums 

通常情況下,你也可以重定向到user_album給ALS以及用戶作爲專輯:

redirect_to user_album_url(@photo.album.user,@photo.album) 

但在您的模型中,沒有單一的user一個album。所以你的路線不反映你的模型關係。 (你的路線暗示那麼album屬於一個user,你的模型說,albumhas_and_belongs_to_manyusers