newbie: issues with updating / redirect profile and friendly_id
-
14-11-2019 - |
문제
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
해결책
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