登录不在我的rails应用程序上工作

时间:2012-12-07 09:21:42

标签: ruby-on-rails ruby-on-rails-3 login

我正在关于twitter克隆的Charles Max木材教程。我收到此错误RuntimeError in HomeController#show

undefined method `all_flits' for nil:NilClass

但是当我在控制台上尝试时,该方法可以正常工作

提高session.inspect我得到

{"return_to"=>"http://localhost:3000/sessions", "session_id"=>"2baca34a681d6a9e259991481891f7bf", "_csrf_token"=>"pKRBBE3m4+T+aTeBBFvqDsEsKxPaWG5AxF3hBiWlV2E="}

我使用ryan bates nifty generator登录。谁一直在工作到现在。我不知道我在show方法中得到的错误。

登录网址为http://localhost:3000/login 我得到的错误是在网址http://localhost:3000/sessions 在登录时我想去http://localhost:3000 这对应于'home #index'

的HomeController

class HomeController < ApplicationController
  before_filter :login_required

  def index
    @flits = current_user.all_flits
    @last_flits = current_user.flits.last
  end

     def show
        @user =  User.find_by_username(params[:username])
        @flits= @user.all_flits
      end

   def toggle_follow
        @user =  User.find_by_username(params[:username])
        if current_user.is_friend? @user
          flash[:notice] = "You are no longer following @#{@user.username}"
          current_user.remove_friend(@user)
        else
          flash[:notice] = "You are now following @#{@user.username}"
          current_user.add_friend(@user)
        end
        redirect_to show_path(@user.username)

     end
end

Show.html.erb

<h1><%= image_tag @user.gravatar_url, :align => "top" %> <%= @user.username %></h1>

<%= form_tag  toggle_follow_path do  %>
  <% if current_user.is_friend? @user %>
     <%=h submit_tag "Following"  , :class => "button" %>
  <% else %>
     <%=h submit_tag "Not Following"  , :class => "button" %>
  <% end %>
<% end %>
<%=h render :partial => "flits_list", :locals => { :flits => @flits }%>

模型User.rb

class User < ActiveRecord::Base
  # new columns need to be added here to be writable through mass assignment
  attr_accessible :username, :email, :password, :password_confirmation

  # gravatastic
  include Gravtastic
  gravtastic 
  has_gravatar :size => 50


  attr_accessor :password
  before_save :prepare_password

  validates_presence_of :username
  validates_uniqueness_of :username, :email, :allow_blank => true
  validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_presence_of :password, :on => :create
  validates_confirmation_of :password
  validates_length_of :password, :minimum => 4, :allow_blank => true


  has_many :flits, :dependent => :destroy

  has_many :friendships
  has_many :friends, :through => :friendships




   def add_friend(friend)
      friendship = friendships.build(:friend_id => friend.id)
       if !friendship.save
         logger.debug "User '#{friend.email}' already exists in the user's friendship list."
       end
   end

     def remove_friend(friend)
           friendship = Friendship.find(:first, :conditions  => ["user_id = ? and friend_id = ?", self.id, friend.id])
           # friendship = self.friendships.find(:friend_id => friend.id)
           if friendship
             friendship.destroy
           end
         end

         def is_friend?(friend)
           return self.friends.include? friend
         end

      def all_flits
           Flit.find(:all, :conditions => ["user_id in (?)", friends.map(&:id).push(self.id)], :order => "created_at desc")
      end

  # login can be either username or email address
  def self.authenticate(login, pass)
    user = find_by_username(login) || find_by_email(login)
    return user if user && user.password_hash == user.encrypt_password(pass)
  end

  def encrypt_password(pass)
    BCrypt::Engine.hash_secret(pass, password_salt)
  end

  private

  def prepare_password
    unless password.blank?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = encrypt_password(password)
    end
  end
end

路线档案

Flitter2::Application.routes.draw do

  root to: 'home#index'
  match 'user/edit' => 'users#edit', :as => :edit_current_user

  match 'signup' => 'users#new', :as => :signup

  match 'logout' => 'sessions#destroy', :as => :logout

  match 'login' => 'sessions#new', :as => :login

  match '/:username',       to: 'home#show', as: 'show'

 match '/:username/toggle_follow', to: 'home#toggle_follow', as: 'toggle_follow'


  resources :sessions

  resources :users

  resources :welcome

  resources :flits
end

Ryan bates controller_authetication module

module ControllerAuthentication
  def self.included(controller)
    controller.send :helper_method, :current_user, :logged_in?, :redirect_to_target_or_default
  end

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def logged_in?
    current_user
  end

  def login_required
    unless logged_in?
      store_target_location
      redirect_to login_url, :alert => "You must first log in or sign up before accessing this page."
    end
  end

  def redirect_to_target_or_default(default, *args)
    redirect_to(session[:return_to] || default, *args)
    session[:return_to] = nil
  end

  private

  def store_target_location
    session[:return_to] = request.url
  end
end

宝石文件

source 'https://rubygems.org'

gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

gem "nifty-generators", :group => :development
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

gem "bcrypt-ruby", :require => "bcrypt"
gem "mocha", :group => :test

gem 'faker'
gem 'populator'
gem 'gravtastic'

2 个答案:

答案 0 :(得分:0)

您想告诉我你是如何获得“current_user”变量的吗?我对此表示错误。在成功登录后,我在代码中看不到存储用户的任何行。

答案 1 :(得分:0)

我也遇到了这个问题然后我可以解决它的一些步骤 1.路线的优先顺序

match '/edit_current_user' => 'users#edit', :as => :edit_current_user, :via => :POST
match '/signup' => 'users#new', :as => :signup, :via => :GET
match '/logout' => 'sessions#destroy', :as => :logout, :via => :GET
match '/login' => 'sessions#new', :as => :login, :via => :GET