views:

242

answers:

1

I'm new to rails!

Ok, I am trying to set up a user signup form. It is mapped as a singular resource in the routes

map.resource :user

And trying to create the user through the console works fine. the controller code for user's signup is as follows:

  def signup
    @user = User.new#(params[:user])
  end

 def create
   @user = User.new(params[:user])
   #debugger
   if request.post?  
     if @user.save
       session[:user] = User.authenticate(@user.login, @user.password)
       flash[:message] = "Signup successful"
       redirect_to registries_path          
     else
       flash[:warning] = "Signup unsuccessful"
       #redirect_to 'user/signup'
     end
   end
  end

The signup view is as follows (and this is where i think something is going wrong)

<% form_tag user_path do %>
<p>User creation.</p>
<p><%= error_messages_for 'user' %></p>

<p>
    <label>Username:</label>
    <%= text_field_tag 'login', nil, :size => 20, :maxlength => 20 %>
</p>
<p>
    <label>Password:</label>
    <%= password_field_tag 'password', nil, :size => 20, :maxlength => 20 %>
</p>
<p>
    <label>Password confirmation:</label>
    <%= password_field_tag 'password_confirmation', nil, :size => 20, :maxlength => 20 %>
</p>
<p>
    <label>Email:</label>
    <%= text_field_tag 'email' %>
</p>
<p><%= submit_tag 'Signup' %></p>
<% end %>

Now, that page renders just fine. I've called the form on the "user_path" which is singular (i think?). But when I hit the submit button, it gives me an error saying that

uninitialized constant UsersController

the occurence of the error makes sense, since User is meant to be singular, so if it is trying to call the Users controller, it should be chucking an error. When I checked the server log, it shows this message:

Processing ApplicationController#create (for 127.0.0.1 at 2010-05-08 16:26:14) [POST]
  Parameters: {"commit"=>"Signup", "password_confirmation"=>"[FILTERED]", "action"=>"create", "authenticity_token"=>"yOcHY+rMjaqmu9HS8EwnDqJKbc0Zxictc0y4dtD26ac=", "controller"=>"users", "login"=>"bob", "password"=>"[FILTERED]", "email"=>"[email protected]"}

NameError (uninitialized constant UsersController):

In the params, I can see that it is calling the "users" controller. But I'm not sure how to fix that, or what is causing it to call the "users" controller as opposed to the "user" controller.

Any ideas? Thanks in advance!

A: 

Users are usually treated as plural, not singular. If you want it to display it as quasi singular in a RESTful url, try setting your routes.rb like this:

map.resources :users,
              :as => 'user'
kingjeffrey
oh.I thought that since there would only be one user at a time (per IP address anyway, or something), a singular resource was the way to go. like how some models use Session as a singular resource since there is only one session active per person?in that case, i might just go to users, and see what happens.thanks!
Jty.tan
You may, for example, allow a user to see other user's profiles.
kingjeffrey
Ok, I went to the plural method of users, and it works the way I'd wanted it to. Thanks! :)Are Sessions normally treated as a singular resource? And if so, is there a simple explanation somewhere why the two are treated differently, and maybe even some info on what is normally treated as a plural and what is normally treated as a singular?
Jty.tan