尝试重写我的配置文件控制器此时已损坏。我使用friendly_id 来获得像下面这样带有部分edit_basics.haml、edit_details.haml 等的url。
- /用户/我/编辑/基础
- /用户/我/编辑/兴趣
- /用户/我/编辑/详细信息
问题在于更新我的个人资料并在更新后重定向到正确的路径。到目前为止,我已经搜索并尝试了几件事无济于事。
- 提交编辑表单后,它重定向到 /profiles/me
更新后 /users/me/edit/basics 它应该返回到这个位置更新引发错误
#<#:0x007f876e77d768> 的未定义方法“update_attributes”
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"wqDebPGdHvXszFvXcaeWwRewA6puTVlv5iCXX1ZD3KU=", "profile"=>{"form"=>"basics", "description"=>" "}, "commit"=>"保存", "id"=>"我的用户名"}
当然ID不能是用户名
路线
match '/users/:username' => "profiles#show"
match '/users/:username/edit/:what' => "profiles#edit", :as => :edit_user
更新操作:
def update
@profile = Profile.where(params[:id])
respond_to do |format|
if @profile.update_attributes(params[:profile])
format.html { redirect_to @profile, :action => "edit", :what => @profile.form, notice: 'Profile was correctly updated.' }
else
format.html { @profile, :action => "edit", :what => @profile.form }
end
end
end
编辑动作:
def edit
@profile = Profile.find(params[:username])
what = params[:what]
if not what.nil?
if ["basics", "location", "details", "photos", "interests"].member?(what)
render :action => "edit_#{what}"
else
render :action => "edit_basics"
end
end
结尾
更新:似乎 id 属性始终包含用户的用户名,因此无法更新