2013-05-27 79 views
2

我已成功建立模型和相冊之間的鏈接。下一步,我添加了一個用戶模型,並建立了與相同模型的連接。它的工作原理,用戶可以創建相冊和上傳照片,但如果保羅想要把用戶鏈接到相冊,希望它看起來像這樣在mysite.com/user/1/albums/1
在routes.rb中添加以下代碼Rails 3.2資源和url_option()

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

在模板中添加圖像的代碼是一個專輯(albums/show.html.erb)。之後,我添加的資源:用戶我得到一個錯誤

undefined method `album_photos_path' 

,參照本紅寶石線

<H1>ADD NEW IMAGE</H1> 
    <%= form_for [@album, @photo], :html => {:multipart => true} do |form| %> 

有人建議我申請url_option但我無法弄清楚它是如何工作的。

在PhotosController

def create 
    @photo = @album.photos.new(params[:photo]) 
    @photo.user_id = current_user.id 
    if @photo.save 
    flash[:notice] = "Photo created!" 
    redirect_to album_url(@album) 
    else 
    flash[:notice] = "No created" 
    redirect_to album_url(@album) 
    end 
end 

高清創建有一個優雅的解決我的問題?

+0

看看'耙routes'輸出該路徑,這可能是'users_photos_albums_path'。或者使用'form_for [@user,@album,@path]' – mihai

+0

@mihai如果我使用'[@user,@album,@path]'我看到'未定義的方法model_name for NilClass:Class' rake routes 'user_album_photos GET/users /:user_id/albums /:album_id/photos' 但我不知道如何指向一個對象@user在這種情況下:( – technobot

回答

0

根據你的評論,好像你需要這個:

<%= form_for user_album_photos_path(@user, @album, @photo), :html => {:multipart => true} do |form| %> 

和Controller: