我使用設計,並已創建模型配置文件。如何編輯配置文件? (使用設計,並有一個模型配置文件,但編輯方法是給錯誤)
我已將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
嗨,以及我得到這個錯誤「沒有路線匹配{:行動=>」編輯「,:控制器=>」配置文件「,:id =>零)」,我在application.html中使用edit_profile_path(@profile) .erb,耙路也顯示 - edit_profile GET /profiles/:id/edit(.:format)profiles#編輯 所以路徑也存在 – iCyborg
將@profile更改爲current_user.profile(您應該設置at-profile實例變量在控制器操作中,它被調用以顯示包含編輯鏈接的頁面)。如果current_user.profile什麼也沒有返回,你的用戶還沒有任何配置文件.. – Mattherick
抱歉無法得到它,我應該在profiles_controller.rb中編輯什麼?我應該在哪裏設置它?我認爲我們幾乎在那裏 – iCyborg