0

Trying to rewrite my profile controller wich is broken at this point. Im using friendly_id to have urls like below with partials edit_basics.haml, edit_details.haml, etc.

  • /users/me/edit/basics
  • /users/me/edit/interests
  • /users/me/edit/details

The problem is in updating my profile and redirect to the correct path after the update. I have searched and tried several things to no avail so far.

  • after submit edit form it redirects to /profiles/me
  • after updating /users/me/edit/basics it should return to this location The updating throws an error in

    undefined method `update_attributes' for #<#:0x007f876e77d768>

    {"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"wqDebPGdHvXszFvXcaeWwRewA6puTVlv5iCXX1ZD3KU=", "profile"=>{"form"=>"basics", "description"=>""}, "commit"=>"Save", "id"=>"myusername"}

    Ofcourse ID can't be the username

Routes

  match '/users/:username' => "profiles#show"
  match '/users/:username/edit/:what' => "profiles#edit", :as => :edit_user

Update Action:

  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

Edit action:

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

end

UPDATE: It seems that id attribute always contains the username of the user and therefore cannot update

Rubytastic
  • 15,001
  • 18
  • 87
  • 175
  • I think that id is ok - thats the way friendly_id works. ID depends on slug column. In your update action try to use find (Profile.find(params[:id])) – zachar Feb 29 '12 at 10:39
  • not sure, but i think part of the problem comes from `Profile.where(params[:id])` in your update action. This will return a relation, not a record. Extra tip : use `unless` instead of `if not`, its more rubyish and arguabily more clear. – m_x Feb 29 '12 at 11:00
  • @_x I thought the same about the `if not` statement. And also I would add that you could do something like `match '/users/:username/edit(/:what)' => "profiles#edit", :defaults => { :what => "basic" }, :as => :edit_user` to allow for normal restful style urls, while still accommodation the more explanatory ones. – Ekampp Feb 29 '12 at 12:39

1 Answers1

1

Try updating this line:

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

In your controller to this, and see if that helps:

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

That will return the instance of the Profile, not the criteria.

Hope it helps.

\\ Emil

Ekampp
  • 755
  • 1
  • 6
  • 9
  • 2
    Better to use `@profile = Profile.find(params[:id])` + small refactor to the `if not what.nil?` section can be found here ... https://gist.github.com/1964911 - probably just a preference thing, but `.present?` is defined as `!blank?` which checks both `.nil?` and `.empty?` – Matenia Rossides Mar 03 '12 at 07:43