2013-04-29 47 views
0

我正在構建一個簡單的應用程序,該應用程序具有典型的用戶模型&個人檔案模型。用戶has_one個人資料和個人資料belongs_to用戶。所有人似乎都工作得很好,因爲我基本上遵循Michael Hartl教程。但是,當我嘗試從配置文件表格(show action)呈現某個視圖的視圖時,出現錯誤(無標識),並且我創建的配置文件記錄被刪除!Rails - 相關模型的展示操作及其相應視圖?

問題:

  1. 在我ProfilesController,我在定義我的表演動作正確的簡單視圖我想呈現?

  2. 爲什麼簡單地訪問url localhost/3000/profiles/1刪除配置文件記錄?我認爲它與依賴銷燬(B/C刪除,這將阻止此行爲)有關,但我想我想保持依賴銷燬,正確嗎?

路線

resources :users 
resources :profiles 

模型

Class User < ActiveRecord::Base 
has_one :profile, dependent: :destroy 

Class Profile < ActiveRecord::Base 
belongs_to :user 

ProfilesController

def new 
    @profile = current_user.build_profile 
end 

def create 
    @profile = current_user.build_profile(params[:profile]) 
    if @profile.save 
    flash[:success] = "Profile created dude!" 
    redirect_to root_path 
    else 
    render 'new' 
    end 
end 

def show 
    @profile = Profile.find(params[:user_id]) 
end 

視圖(輪廓/ show.html.erb)

<p>Display Name: <%= @profile.display_name %></p> 

回答

0

檢查您的rake routes。您會看到,對於您的Profile#show,您的URL結構如下:/profiles/show/:id
因此,params必須期待:id而不是:user_id

如果/profiles/show/3,你希望顯示的個人資料3,則:

def show 
    @profile = Profile.find(params[:id]) 
end