Topic: restful_authentication question

Hello all,

I am getting this error  "undefined method `login' for 2:Fixnum"
this is where <p><strong>You are logged in as <%=h current_user.login %></strong></p>
in my rhtml

my routes.rb looks like this
  map.resources :users, :sessions
  map.resource :session, :controller => 'sessions'
  map.signup '/signup', :controller => 'users', :action => 'new'
  map.login  '/login', :controller => 'session', :action => 'new'
  map.logout '/logout', :controller => 'session', :action => 'destroy'


It works when I do
http://localhost:3000/login and http://localhost:3000/signup

but doesn't when I do
http://localhost:3002/logout and http://localhost:3002/home

Any ideas guys?

thanks for your help!

Re: restful_authentication question

Do you have a SessionController or SessionsController? Looks like you're trying to use both in the routes. You should go with one or the other.

Railscasts - Free Ruby on Rails Screencasts

Re: restful_authentication question

thanks for that Ryan

I have "SessionsController"

I did changed it to
  map.resources :users
  map.resource :session, :controller => 'sessions'
  map.signup '/signup', :controller => 'users', :action => 'new'
  map.login  '/login', :controller => 'session', :action => 'new'
  map.logout '/logout', :controller => 'session', :action => 'destroy'

but I am still getting the error "undefined method `login' for 2:Fixnum"

this is my Full Trace --------------------------------------------

#{RAILS_ROOT}/app/views/home/index.rhtml:4:in `_run_rhtml_47app47views47home47index46rhtml'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:326:in `send'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:326:in `compile_and_render_template'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:301:in `render_template'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:260:in `render_file'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:806:in `render_file'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:711:in `render_with_no_layout'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/layout.rb:247:in `render_without_benchmark'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:50:in `render_without_active_scaffold'
C:/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:50:in `render_without_active_scaffold'
#{RAILS_ROOT}/vendor/plugins/active_scaffold/lib/extensions/action_controller.rb:13:in `render'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1096:in `perform_action_without_filters'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:638:in `call_filter'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:438:in `call'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:637:in `call_filter'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:638:in `call_filter'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:438:in `call'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:637:in `call_filter'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
C:/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in `perform_action'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `send'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `process_without_filters'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in `process'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in `process'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:113:in `handle_dispatch'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in `service'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/InstantRails/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in `dispatch'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/webrick.rb:59
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
script/server:3
-e:4:in `load'
-e:4

Re: restful_authentication question

Try this:

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

I'm not sure if that's your problem though. It looks like it's occurring in home/index.rhtml. What's in there?

Railscasts - Free Ruby on Rails Screencasts

Re: restful_authentication question

thanks Ryan

I tried every combination.  I did try that just in case but it's no go.
I am actually following your railscast.

I am adding it to my existing project which makes it more difficult.
I may remove all the files and try it again from the start.


home.rhtml -- just like the one from your cast.
<% if logged_in? %>
  <p><strong>You are logged in as <%=h current_user.login %></strong></p>
  <p><%= link_to 'Logout', logout_path %></p>
<% else %>
  <p><strong>You are currently not logged in.</strong></p>
  <p>
    <%= link_to 'Login', login_path %> or
    <%= link_to 'Sign Up', signup_path %>
  </p>
<% end %>

Re: restful_authentication question

Do you have an existing method called "current_user"? If so this may be overriding the one provided by restful_authentication.

Railscasts - Free Ruby on Rails Screencasts

Re: restful_authentication question

It looks like you have an existing method called current_user that is returning the ID of the user instead of the user object for that ID.

Re: restful_authentication question

I followed the instructions as indicated in Ryan's railscast.

After I installed the restful-authentication plugin, I created home_controller.rb and views/home/index.rhtml because they were not there.

I also changed routes.rb to have this line:  map.home '', :controller => 'home', :action => 'index'.  So, routes.rb is as follows:

ActionController::Routing::Routes.draw do |map|
    map.home '', :controller => 'home', :action => 'index'
   
    map.resources :users
    map.resource :session, :controller => 'session'
    map.signup     '/signup', :controller => 'users', :action => 'new'
    map.login     '/login', :controller => 'session', :action => 'new'
    map.logout     '/logout', :controller => 'session', :action => 'destroy'
end

My problem is that I do not see the "Welcome" page (/home/index.rhtml):
1.  When I entered the wrong password, I did see error message "Authentication failed".  But, when I entered the correct password, I was re-directed to "http://localhost:3000/", but it does not display the Welcome page (/home/index.rhtml).

2.  When I entered "http://localhost:3000/logout", session/destroy was run and I was redirected to "http://localhost:3000/", but it does not display the Welcome page (/home/index.rhtml).

login and signup work fine.

Pls do help.  Thanks very much.

Re: restful_authentication question

By the way, I am using Instant Rails, which is using Rails v1.2.3.

Re: restful_authentication question

What does it display instead of the welcome page? Make sure you remove the file at public/index.html.

Railscasts - Free Ruby on Rails Screencasts

Re: restful_authentication question

Good!  It works after I remove the public/index.html file.  Thanks much.