Reputation: 10224
I'm trying to create a custom action for a Update My Profile Page. It can't be the regular users#edit or users#update action because only Admins can use that page.
So this is what I have in routes.rb (side question: how can I make the link become mydomain.com/users/update_profile instead of mydomain.com/update_profile?
get 'update_profile/:id' => "users#update_profile", :as => 'update_profile'
_form.html.erb
<%= form_for @user, :url => update_profile_path(@user) do |f| %>
<%= render :partial => "form", :locals => {:f => f} %>
<% end %>
users_controller.rb
def update_profile
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
else
format.html { render action: "edit" }
end
end
end
When I submit the form I get this: Routing Error No route matches [PUT] "/update_profile/1"
I understand the error because according to the output of rake routes
the update_profile action is GET.
So I need to change it to PUT, but I don't know how. How can I setup a custom PUT action in routes.rb?
Upvotes: 2
Views: 5001
Reputation: 2459
You should add it as a member of your users
resource
resources :users do
member do
put :update_profile
end
end
This way it will still be part of your users resource and the route will be automatically generated and be accessible in your form through something like update_profile_user_path(@user)
Upvotes: 4