Why is this action being called when I'm not calling it?

Hi,

I'm submitting a form to the "userconfirm" action of my
register_controller.rb file:

        def userconfirm
                @user = User.new(params[:user])
                if @user.save
                        # Create session
                        redirect_to :controller => 'sessions', :action
=> 'create', :login => params[:login], :password => params[:password]
                else
                        render :action => 'userinfo'
                end
        end

But for some reason, I'm getting redirected to "/sesssions" (according
to the log file) and here's the error I'm getting

Unknown action
No action responded to show

Below is my config/routes.rb file. Why am I getting this error when I
clearly redirect to the "create" action? Log file entry follows. -
Dave

=====================begin routes.rb=========================
ActionController::Routing::Routes.draw do |map|

  map.connect '', :controller => "register", :action => "start"

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

  # The priority is based upon order of creation: first created ->
highest priority.

  # Sample of regular route:
  # map.connect 'products/:id', :controller => 'catalog', :action =>
'view'
  # Keep in mind you can assign values other than :controller
and :action

  # Sample of named route:
  # map.purchase 'products/:id/purchase', :controller =>
'catalog', :action => 'purchase'
  # This route can be invoked with purchase_url(:id => product.id)

  # You can have the root of your site routed by hooking up ''
  # -- just remember to delete public/index.html.
  # map.connect '', :controller => "welcome"

  # Allow downloading Web Service WSDL as a file with an extension
  # instead of a file named 'wsdl'
  map.connect ':controller/service.wsdl', :action => 'wsdl'

  # Install the default route as the lowest priority.
  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action/:id'
end
======================end routes.rb=======================

======================log/development.log==================
Processing RegisterController#userconfirm (for 67.190.94.163 at
2008-01-30 13:23:47) [POST]
  Session ID:
BAh7CToMY3NyZl9pZCIlOTc3ODFlMGVhZWU2MDQ2YWVmOTUyMmI5MWQ0OTlm
%0AYzc6DnJldHVybl90bzA6EHBoYXJtYWN5X2lkIggxMjMiCmZsYXNoSUM6J0Fj
%0AdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA
%3D--4a3c120be536a24d8bc4a5f4236644c47df14731
  Parameters: {"user"=>{"work_phone"=>"123-456-7890",
"work_phone_extension"=>"", "user_type_id"=>"2",
"ship_to_last_name"=>"Alvarado", "ship_to_city"=>"asddfsa",
"password_confirmation"=>"dave", "ship_to_street"=>"asdsdaf",
"ship_to_zip"=>"78727", "phone"=>"303-123-4567",
"ship_to_first_name"=>"Dave", "ship_to_state"=>"TX",
"login"=>"dave304", "password"=>"dave", "email"=>"dave@dave4.com"},
"commit"=>"Create", "action"=>"userconfirm", "controller"=>"register"}
  ^[[4;36;1mUser Columns (0.005564)^[[0m ^[[0;1mSHOW FIELDS FROM
`users`^[[0m
  ^[[4;35;1mSQL (0.000205)^[[0m ^[[0mBEGIN^[[0m
  ^[[4;36;1mUser Load (0.003407)^[[0m ^[[0;1mSELECT * FROM `users`
WHERE (LOWER(users.login) = 'dave304') LIMIT 1^[[0m
  ^[[4;35;1mUser Load (0.003357)^[[0m ^[[0mSELECT * FROM `users`
WHERE (LOWER(users.email) = 'dave@dave4.com') LIMIT 1^[[0m
VALIDATES_AS_PHONE: 3031234567 is the value 10 valid? true
  ^[[4;36;1mUser Create (0.037157)^[[0m ^[[0;1mINSERT INTO `users`
(`created_on`, `last_login`, `pharmacy_id`, `provincial_tax`, `salt`,
`tax`, `work_phone`, `work_phone_extension`, `cc_company`,
`cc_number`, `discount`, `ship_to_city`, `ship_to_last_name`,
`user_type_id`, `cc_security_code`, `comments`, `company_name`,
`crypted_password`, `bill_to_attn`, `bill_to_country`,
`bill_to_street2`, `demo`, `ship_to_country`, `updated_on`,
`dateofbirth`, `html_email_pref`, `lock_order_form_prices`,
`remember_token_expires_at`, `ship_to_street`, `ship_to_zip`,
`show_retail_prices`, `user_group_id`, `bill_to_city`, `enabled`,
`bill_to_company`, `user_agent`, `fax`, `phone`, `remember_token`,
`ship_to_attn`, `ship_to_street2`, `shipping`, `bill_to_last_name`,
`bill_to_street`, `cc_exp_date`, `reseller_id`, `user_id`,
`bill_to_state`, `login`, `ship_to_first_name`, `ship_to_state`,
`bill_to_first_name`, `bill_to_zip`, `email`, `po_num`,
`ship_to_company`) VALUES('2008-01-30 13:23:47', NULL, NULL, 0.0,
'6a71ee10804a17fee07e2c524dba2039971d2b3b', 0.0, '123-456-7890', '',
NULL, NULL, 0.0, 'asddfsa', 'Alvarado', 2, NULL, NULL, '',
'7af9ccb951cf54803b5761251990d2550a402448', NULL, NULL, NULL, 0, NULL,
'2008-01-30 13:23:47', NULL, 1, 0, NULL, 'asdsdaf', '78727', 1, NULL,
NULL, 1, NULL, NULL, NULL, '303-123-4567', NULL, NULL, NULL, 0.0,
NULL, NULL, NULL, NULL, NULL, NULL, 'dave304', 'Dave', 'TX', NULL,
NULL, 'dave@dave4.com', NULL, NULL)^[[0m
  ^[[4;35;1mSQL (0.006645)^[[0m ^[[0mCOMMIT^[[0m
Redirected to http://remandev.no-ip.org:3000/sessions
Completed in 0.08216 (12 reqs/sec) | DB: 0.05633 (68%) | 302 Found
[http://remandev.no-ip.org/register/userconfirm]
=======================end entry in log======================

Let me understand the problem here. You're getting redirected to
sesssions, with three s's? No wait, that was just a typo in your first
paragraph.

It looks like, umm, you're telling your Rails program to redirect to
sessions.
redirect_to :controller => 'sessions', :action => 'create', :login =>
params[:login], :password => params[:password]

:controller => 'sessions' goes to site.com/sessions.
How about
redirect_to :controller => 'SOME CONTROLLER', :action =>
'create', :login => params[:login], :password => params[:password]

That the problem?

-Ryan

Just a guess
but in your routes.rb there's

  map.resource :sessions

shouldn't be
  map.resources :sessions ?

Ah, well I read that as though the error was that you were getting
redirected to where you don't want to be. But yeah, I think that
map.resources may have some effect.

Hi,

I did change the entry in my config/routes.rb file from

  map.resource :sessions

to

  map.resources :sessions

and now I get the error:

Unknown action
No action responded to index

Ryan, to your point, yes, typo with "sesssions", but also I do have a
sessions_controller.rb file with a create method, which I'm wanting to
invoke. But clearly, I'm not understanding something about
redirection. Your saying that in my register_controller.rb file, a
call to

                        redirect_to :controller => 'sessions', :action
=> 'create', :login => params[:login], :password => params[:password]

ignores the "create" action and just jumps straight to the default
action for "sessions"? - Dave

laredotornado@zipmail.com wrote:

Hi,

I did change the entry in my config/routes.rb file from

  map.resource :sessions

to

  map.resources :sessions

and now I get the error:

Unknown action
No action responded to index

Ryan, to your point, yes, typo with "sesssions", but also I do have a
sessions_controller.rb file with a create method, which I'm wanting to
invoke. But clearly, I'm not understanding something about
redirection. Your saying that in my register_controller.rb file, a
call to

                        redirect_to :controller => 'sessions', :action
=> 'create', :login => params[:login], :password => params[:password]

ignores the "create" action and just jumps straight to the default
action for "sessions"? - Dave

It looks like you are switching controllers from register to sessions.
Is there an method called 'create' under the 'sessions' controller? And
are you meaning to switch controllers?

-S

Hi,

Yes, there is the switch of controllers is intentional. That's the
right way to redirect to a different controller right? And there's a
method in my app/controllers/sessions_controller.rb file called
"create". Here's the code:

=================Begin sessions_controller.rb
file============================
# This controller handles the login/logout function of the site.
class SessionsController < ApplicationController
  # render new.rhtml
  def new
  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
      # The next two lines were added 1/24/08
      @user = User.find(:first, :conditions => ["login = ?",
params[:login]])
      session[:user_id] = @user.id
      # redirect_back_or_default('/')
      flash[:notice] = "Logged in successfully"
      redirect_to :controller => 'order', :action => 'start'
    else
      render :action => 'abcdff'
    end
  end

  def destroy
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = "You have been logged out."
    redirect_back_or_default('/')
  end
end
=================End sessions_controller.rb
file=============================

Do you have any before filters which might be redirecting?

It looks like you're basing this off of restful_authentication.

Yes, there is the switch of controllers is intentional. That's the
right way to redirect to a different controller right? And there's a
method in my app/controllers/sessions_controller.rb file called
"create". Here's the code:

=================Begin sessions_controller.rb
file============================
# This controller handles the login/logout function of the site.
class SessionsController < ApplicationController
  # render new.rhtml
  def new
  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
      # The next two lines were added 1/24/08
      @user = User.find(:first, :conditions => ["login = ?",
params[:login]])
      session[:user_id] = @user.id

What's this for? Why isn't using the current_user method provided by
the restful_authentication generated code good enough?
User.authenticate(params[:login] should have set this up.

      # redirect_back_or_default('/')
      flash[:notice] = "Logged in successfully"
      redirect_to :controller => 'order', :action => 'start'
    else
      render :action => 'abcdff'

What's 'abcdff'???

    end
  end

> > Hi,
>
> > I did change the entry in my config/routes.rb file from
>
> > map.resource :sessions
>
> > to
>
> > map.resources :sessions

I don't know if it relates to the problem but when I use
restful_authentication I usually name the controller SessionController
(singular) and have this in routes.rb:

map.resource :session, :controller=> 'session'

Yes, this is based off restful_authentication.

I don't remember putting in a before filter. How do I check for that?

What I'm trying to do is when a user registers, I want to auto-log
them in after I've created their user record. Since the login
mechanism is in the sessions controller's "create" action, I thought I
could redirect to there, but redirecting is proving to be tricky! -
Dave

@Dave,

The essential stuff that SessionsController.create is doing is putting
the user id of the authenticated user into session[:userid]. Since it
appears to be your intention that you immediately login the newly
created user and then redirect to an order page (the redirect inserted
into Sessions.create), it'd probably be much simpler to add

session[:userid] = @user.id

in the place of your current redirect in the
RegisterController.userconfirm action and then have that method
redirect to the Order. If nothing else you'll eliminate one
(unnecessary) redirect. The other work that's being done (remember me
token, etc) is not essential here; I'd suggest letting them use it
when they have an account they use to sign in in the future but
ignoring it here.

HTH,
AndyV

Thanks for this advice, Andy. That is a good idea, but isn't the
"current_user" attribute of the session also being set?

  def create
    self.current_user = User.authenticate(params[:login],
params[:password])
    ...
    session[:user_id] = @user.id
    ...
  end

If I'm not in the sessions controller, how do I duplicate that
functionality from the register controller?

As a follow up, should I just put all the session create method code
minus the redirects into a separate module so both register and
session controller can access it?

- Dave

Dave,

If you've included AuthenticatedSystem into your ApplicationController
then it takes care of setting the current_user attribute for you.

Andy

Althouhg I have included that in my ApplicationController, sadly, it
doesn't appear that creating a user automatically logs them in. In my
RegisterController I have

        def userconfirm
                @user = User.new(params[:user])
                if @user.save
                        redirect_to :controller => 'order', :action =>
'new'
                else
                        render :action => 'userinfo'
                end
        end

and in the OrderController's new method, I have:

        def new
            if logged_in?
                @user = User.find(session[:user_id])
                @ec_order = EcOrder.new
                5.times { @ec_order.ec_line_items.build }
            else
                flash[:notice] = "You must be logged in to access this
page."
                redirect_to :controller => "register", :action =>
"start"
            end
        end

And after creating a new user, I'm redirected back to the start page
with the above error message instead of being kept on the order page.

- Dave

You'll need this:

def userconfirm
  @user = User.new(params[:user])
  if @user.save
    session[:userid] = @user.id
    redirect_to :controller => 'order', :action => 'new'
  else
    render :action => 'userinfo'
  end
end

Adding the line that sets session[:userid] is what creates the effect
of automatically logging in the user. You can omit the
self.current_user assignment in the userconfirm action, though. One
other note... if you're using rails 2.x then your redirect can be
reduced to "redirect_to new_order_path". It's both shorter and easier
to read.

In your OrdersController#new action you should not need the @user
assignment. That is what AuthenticatedSystem should do for you (given
that you've assigned it as shown above). Similarly, the logged_in?
guard is redundant; it's already done in the before_filter in the
ApplicationController (and the root cause of why you're getting thrown
back to the start page). Whittling away, you should be able to get
away with:

def new
  @user = self.current_user
  @ec_order = EcOrder.new
  5.times { @ec_order.ec_line_items.build }
end

It's worth noting that the @user assignment is probably superflous...
refer to @current_user in your view instead.