新手:更新/重定向配置文件和friendly_id的问题

时间:2012-02-29 09:10:34

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 scaffolding friendly-id

此时尝试重写我的配置文件控制器。我使用friendly_id来获取下面的网址,包括部分编辑edit_basics.haml,edit_details.haml等。

  • /用户/我/编辑/基础知识
  • /用户/我/编辑/利益
  • /用户/我/编辑/细节

问题在于更新我的个人资料并在更新后重定向到正确的路径。 到目前为止,我已经搜索并尝试了几件事无济于事。

  • 提交编辑表单后,它会重定向到/ profiles / me
  • 更新/ users / me / edit / basics后,它应该返回此位置 更新会在

    中引发错误

    #<#:0x007f876e77d768>

    的未定义方法`update_attributes'

    { “UTF8”=> “中✓”,  “_method”=> “中放”,  “authenticity_token”=> “中wqDebPGdHvXszFvXcaeWwRewA6puTVlv5iCXX1ZD3KU =”,  “简档”=> { “形式”=> “中基”,  “描述”=> “中”},  “提交”=> “中保存”,  “ID”=> “中名为myUsername”}

    Ofcourse 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属性始终包含用户的用户名,因此无法更新

1 个答案:

答案 0 :(得分:1)

尝试更新此行:

@profile = Profile.where(params[:id])

在你的控制器中,看看是否有帮助:

@profile = Profile.where({ :id => params[:id] }).first

这将返回个人资料的实例,而不是标准。

希望它有所帮助。

\\ Emil