我使用设计,并已创建模型配置文件。如何编辑配置文件? (使用设计,并有一个模型配置文件,但编辑方法是给错误)
我已将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