Question

I was trying to create a update form for the registered users in my applcation

View:

<h2><%= @title =%></h2>

<% if @user.errors.any? %>
<div id="error">
<h2><%= pluralize(@user.errors.count,"error") %> prohibited this user from registering</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg%></li>
<%end%>
</ul>
</div>
<%end%>

<%= form_for :user do |form| %>

<div class="reg_form">
<label for="first_name">First Name: </label>
<%= form.text_field :first_name %>
</div>
<div class="reg_form">  
<label for = "last_name">Last Name: </label>
<%= form.text_field  :last_name %>
</div>
<div class="reg_form">
<%=form.submit "Update"%>
</div> 
<%end%>

Controller: users_controller.rb

def index
    unless session[:user_id]
        flash[:notice] = "Please Login first"
        redirect_to :action => "new"
    end
    @title = "Profile"
    @user=User.find(session[:user_id])
end


def edit
    @title="Profile Update"
    @user = User.find(session[:user_id])
    if param_posted?(:user)
        if @user.update_attributes(params[:user])
            flash[:notice] = "Update Successful"
            redirect_to :controller => "user", :action => "profile"
        end
    end
end

I included get "edit"=> 'users#edit' in routes.rb. However, I get an error 'No route matches [POST] "/users/edit"'

Any help is appreciated. Thank you.

Was it helpful?

Solution

The RESTful edit route should use a GET verb. The vanilla resource#edit method doesn't change the database, but simply puts up a form populated with the selected resource's values. Submitting that form to resource#update (using the PUT verb) does the work of actually changing the database.

So, your users#edit method isn't doing what a standard RESTful edit action does. I'd suggest using the Standard Way unless you have a really good reason and you document that reason carefully. I concur that spending time looking at rails routing guides and a vanilla controller would be useful.

OTHER TIPS

You need a POST route to handle the form submission. Try resources :users in routes.rb and do a rake routes in command prompt to see all your available routes.

I recommend you review the Rails Guides and look at all the methods created using scaffold and how Rails handles the routing for different things to each of the methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top