2013-08-17 23 views
0

我使用設計,並已創建模型配置文件。如何編輯配置文件? (使用設計,並有一個模型配置文件,但編輯方法是給錯誤)

我已將has_one關係添加到用戶和belongs_to:用戶到配置文件模型。我還在配置文件中添加了user_id列。

我也曾在用戶模型中添加

before_filter :create_profile 
def create_profile 
    profile = Profile.create 
end 

。當新用戶註冊時,新的配置文件將被創建。因此,如果用戶ID是10,這是在輪廓越來越創建

#<Profile id: 2, name: nil, created_at: "2013-08-17 06:44:33", updated_at: "2013-08-17 06:47:00", user_id: 10> 

現在的問題是,我已經把編輯鏈接application.rb中

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li> 

,但它給錯誤

Couldn't find Profile with id=10 

因此,而不是搜索id = 2,它正在查看user_id,如何解決這個問題?我相信我錯過了一些非常小但不知道是什麼的東西。

編輯 - 誤差從該線到來@profile = Profile.find(PARAMS [:ID])在編輯方法

# profiles_controller.rb 
def edit 
    @profile = Profile.find(params[:id]) 
end 

我曾嘗試使用:USER_ID和:PROFILE_ID但也給錯誤 「無法找到沒有ID的個人資料」

EDIT 2 -

的routes.rb有,這是相當多的空白

resources :profiles 

耙路輸出(其它路徑是制定標準路徑)

   profiles GET  /profiles(.:format)          profiles#index 
         POST  /profiles(.:format)          profiles#create 
      new_profile GET  /profiles/new(.:format)         profiles#new 
      edit_profile GET  /profiles/:id/edit(.:format)       profiles#edit 
       profile GET  /profiles/:id(.:format)         profiles#show 
         PUT  /profiles/:id(.:format)         profiles#update 
         DELETE  /profiles/:id(.:format)         profiles#destroy 

profiles_controller.rb外,其他5個方法是標準的一次

def edit 
    @user = current_user 
    @profile = @user.profiles.find(params[:profile_id]) 

    end 

    def create 

    @profile = current_user.build_profile(params[:profile]) 

    respond_to do |format| 
     if @profile.save 
     format.html { redirect_to @profile, notice: 'User profile was successfully created.' } 
     format.json { render json: @profile, status: :created, location: @profile } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @profile.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

回答

2

首先解決您的用戶模型:

before_filter :create_profile 
def create_profile 
    profile = Profile.create 
end 

應該是:

after_create :create_profile 

def create_profile 
    self.profile.create 
end 

的before_filter是控制器,而不是模型!

二:這一行

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li> 

更改爲這一個:

<li><%= link_to "Profile", edit_profile_path(current_user.profile) %></li> 

三:改變你的控制器編輯操作:

def edit 
    @profile = current_user.profile 
end 

我想你可以刪除創建行動從你的profiles_controller中,因爲你的用戶模型中創建了before_create鉤子個人資料。

你的路線

resources :profiles 

只是爲您提供了編輯操作的id參數,並that's的ID從配置文件,而不是從用戶。您可以通過嵌套路由爲current_user添加一個,但這不是必需的,因爲您已經擁有了設計中的current_user方法,它會返回當前登錄的用戶。

順便說一句:

"Profile".html_safe 

=>是沒有必要的。

"<h1>Profile</h1>".html_safe 

=>將是必要的。

+0

嗨,以及我得到這個錯誤「沒有路線匹配{:行動=>」編輯「,:控制器=>」配置文件「,:id =>零)」,我在application.html中使用edit_profile_path(@profile) .erb,耙路也顯示 - edit_profile GET /profiles/:id/edit(.:format)profiles#編輯 所以路徑也存在 – iCyborg

+0

將@profile更改爲current_user.profile(您應該設置at-profile實例變量在控制器操作中,它被調用以顯示包含編輯鏈接的頁面)。如果current_user.profile什麼也沒有返回,你的用戶還沒有任何配置文件.. – Mattherick

+0

抱歉無法得到它,我應該在profiles_controller.rb中編輯什麼?我應該在哪裏設置它?我認爲我們幾乎在那裏 – iCyborg

1

不知道你的控制器看起來像,但你可以明確地告訴rails哪裏可以找到profile_id。是這樣的:

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user, profile_id: @profile) %></li> 

編輯
#<Class:0xb459b8c>是整個用戶類,而不是一個實例這就是爲什麼你不能把它profiles。試試這個:

@user = current_user 
@profile = @user.profiles.find(params[:profile_id]) 

編輯2

根據你原來的問題:

的問題是,我已經把應用程序的編輯鏈接。RB

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>

,但它給錯誤

Couldn't find Profile with id=10

所以不是搜索ID = 2,它正在尋找USER_ID,如何解決這一問題?

的問題,我認爲,是在這裏:

`<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>` 

你的`edit_profile_path」呼籲

/profiles/:id/edit(.:format) 

:id是:在profile表的ID,而不是user表 - 所以你的link_to應該看起來像這樣:

<li><%= link_to "Profile".html_safe, edit_profile_path(id: @profile.id) %></li>

或只是

<li><%= link_to "Profile".html_safe, edit_profile_path(@profile) %></li>

和控制器應該是這樣的(它是怎麼做最初):

# profiles_controller.rb 
def edit 
    @profile = Profile.find(params[:id]) 
end 

,這將給你的路由他們想要的東西。

+0

我仍然有同樣的錯誤,「無法找到帶有ID的配置文件」,錯誤是在編輯方法中的這一行(如上所示) @profile = Profile.find(params [:id]) – iCyborg

+0

現在得到這個錯誤「無法找到沒有ID的配置文件」 – iCyborg

+0

現在得到這個錯誤「未定義的方法配置文件」爲#「,我也試過current_user.profiles..find(params [:profile_id ])和user.profiles.find(params [:profile_id]),仍然是相同的錯誤。我檢查了軌道控制檯和所有的數據是在那裏,所以唯一的問題是我們沒有正確的語法來調用編輯 – iCyborg

2

如果設計已經提供了編輯功能,那麼爲什麼你需要維護配置文件,如果你需要它,你不應該在配置文件中添加user_id列,它應該來自用戶。

試試這個:

<%= link_to "edit", edit_user_registration_path(current_user) %> 
+0

嗨,好吧,這是帶我到用戶編輯表單,我需要去個人資料編輯表格 – iCyborg

+0

好,然後定義一個在配置文件控制器中編輯的方法,並遵循dax答案的意見。 – Sami

+0

現在出現此錯誤「找不到沒有ID的配置文件」 – iCyborg