0

试图重写我的配置文件控制器到目前为止已被打破。使用friendly_id林有像谐音edit_basics.haml,edit_details.haml等下面的网址新手:与更新/重定向配置文件和friendly_id问题

  • /用户/我/编辑/基础
  • /用户/我/编辑/利益
  • /用户/ me/edit/details

问题是在更新我的配置文件并在更新后重定向到正确的路径。 我已搜索并尝试了几件事情迄今无济于事。

  • 提交编辑表格后,将其重定向到/型材/我
  • 更新/用户/我/编辑/基础知识后,它应该返回到这个位置 的更新抛出

    未定义的方法错误`的update_attributes'为#<#:0x007f876e77d768>

    { “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 

UPDATE: 似乎id属性始终包含用户的用户名,因此无法更新

+0

我认为id是好的 - 那就是friendly_id的工作方式。 ID取决于slug列。在更新操作中,尝试使用find(Profile.find(params [:id])) – zachar 2012-02-29 10:39:25

+0

不确定,但我认为问题的一部分来自更新操作中的'Profile.where(params [:id])''。这将返回一个关系,而不是记录。额外的提示:使用'除非'而不是'如果不是',它更红宝石,争论更清楚。 – 2012-02-29 11:00:32

+0

@_x我对“if not”语句的想法是一样的。 而且我会补充说你可以做一些类似'match'/ users /:username/edit(/:what)'=>“profiles#edit”,:defaults => {:what =>“basic”} ,:as =>:edit_user'来允许正常的宁静风格的网址,而仍然适应更多的解释性网址。 – Ekampp 2012-02-29 12:39:06

回答

1

尝试更新该行:

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

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

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

这将返回配置文件的实例,而不是条件。

希望它有帮助。

\\ Emil

+2

更好地使用'@profile = Profile.find(params [:id])'+如果不是what.nil?'的小重构,可以在这里找到... https://gist.github.com/ 1964911 - 可能只是一个首选项,但'.present?'被定义为'!blank?',它同时检查'.nil?'和'.empty?' – 2012-03-03 07:43:25