ruby rails - undefined local variable or method `n

2019-09-07 02:47发布

问题:

Running Rails 3. Included "include Rails.application.routes.url_helpers" in model file of users. Still getting this error. Any idea??

# cat app/models/user.rb
class User < ActiveRecord::Base
include Rails.application.routes.url_helpers
acts_as_authentic
end

# rake routes
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
     root        /                         users#index


# cat config/routes.rb | grep -v '#'
www::Application.routes.draw do
 resources :users
end

Error -

Showing /var/www/app/views/users/index.html.erb where line #7 raised:

undefined local variable or method `new_user_session_path' for #<#<Class:0x9a51c6c>:0x9bb6170>
Extracted source (around line #7):

4:   
5: <p>Public Facing Content</p>  
6:   
7: <%= link_to 'Login', new_user_session_path %>

回答1:

I presume you are using Authlogic? Have you generated the user_session class and controller? https://github.com/binarylogic/authlogic_example

Edit: Try adding

resources :user_session

to routes.rb

Edit 2: You are following quite an old tutorial. Now I know what you are trying to achieve I suggest you:

  1. Switch to Devise. Devise seems to have won out as the default rails authentication gem.
  2. Start from scratch and get authentication working properly before trying to do it with Ajax.

    • Devise: https://github.com/plataformatec/devise
    • Tutorial: http://railsapps.github.com/tutorial-rails-devise-rspec-cucumber.html