restful_authentication: getting error when trying to login

Hi,

After downloading the restful_authentication plugin, I have the following in my config/routes.rb file:

  map.signup '/signup', :controller => 'users', :action => 'new'   map.login '/login', :controller => 'session', :action => 'new'   map.logout '/logout', :controller => 'session', :action => 'destroy'

and here is my login form:

<%= form_tag('/login', :method => :post) %> <table>         <tr>                 <td align="right">Login:</td>                 <td align="left"><%= text_field_tag("login", '') %></

        </tr>         <tr>                 <td align="right">Password:</td>                 <td align="left"><%= password_field_tag("password", '') %></td>         </tr>         <tr><td colspan="2"><%= submit_tag("Login") %></td></tr> </table> <%= end_form_tag %>

but after logging in, I'm redirected to "mydomain.com/session" and I get this error:

Unknown action No action responded to show

Oh, and this is the session controller I have ...

class SessionController < ApplicationController   # Be sure to include AuthenticationSystem in Application Controller instead   include AuthenticatedSystem

  # render new.rhtml   def new     redirect_to :action => 'create'   end

  def create     self.current_user = User.authenticate(params[:login], params[:password])     if logged_in?       if params[:remember_me] == "1"         self.current_user.remember_me         cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }       end       redirect_back_or_default('/')       flash[:notice] = "Logged in successfully"     else       render :controller => 'register', :action => 'start'     end   end

Why am I getting this error? - Dave

I encountered the same error messages when I used the restful_authentication plugin and tried to add new actions to my users controller. When I looked at the server log files, I saw this error message:

  Parameters: {"action"=>"show", "id"=>"newaction", "controller"=>"users"}   ActionController::UnknownAction (No action responded to show):

I received these error messages because, per the installation instructions for restful_authentication, I had added this line of code to my routes.rb file:

  map.resources :users, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete }

The call to ActionController::resources here defines the users class as a RESTful resource:

  http://api.rubyonrails.org/classes/ActionController/Resources.html

which means (among other things) that by default the class only supports six actions - new, create, show, edit, update and destroy. Each of these actions uses one of the four basic HTTP verbs - GET, POST, PUT and DELETE.

If we go back to this line from routes.rb:

  map.resources :users, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete }

it appears that our controller should support not only the standard six actions but also a 'suspend', 'unsuspend' and 'purge' action. If you type 'rake routes', you should see routes for these actions. (If you don't, make certain that there is only one "map.resources :users" line in routes.rb) The routes that rake prints for these non-RESTful actions should look something like this:

  suspend_user PUT /users/:id/suspend {:controller=>"users", :action=>"suspend"}   formatted_suspend_user PUT /users/:id/suspend.:format {:controller=>"users", :action=>"suspend"}

Note the :id parameter - these actions all operate on an existing object (which, of course, has an ID). But this won't work for the 'create' action you said you wanted to add to your controller, since you want to create a new object, not modify an existing one. In this case, you should use :collection instead of :member, like so:

  map.resources :users,     :member => { :suspend => :put, :unsuspend => :put, :purge => :delete },     :collection => { :create => :get}

The API documentation for ActionController::resources has more information. Another useful source of information about RESTful interfaces is the "Advanced Rails Recipes" Beta Book:

  Pragmatic Bookshelf: By Developers, For Developers

The book has a chapter entitled "Adding Your Own REST Actions (Or Not)" that provides more detail about how to add custom actions to a RESTful resource that was created by ActionController::Resources.

If you are just interested in renaming the 'new' action to 'create' instead of making up new types of actions, then you could try playing around with these modifications to routes.rb:

  map.login '/login', :controller => 'sessions', :action => 'new'   map.logout '/logout', :controller => 'sessions', :action => 'destroy'

These two examples are printed out when you run the 'authenticated' generator. If you didn't save the output of the generator when you first ran it, just look in this file:

  vendor/plugins/restful_authentication/generators/authenticated/ authenticated_generator.rb

I suspect you don't want to completely get rid of the 'new' action, since this will break some of the RESTful behavior.

Hope this helps,

  craig