0

私は、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 の気の利いたジェネレーターを使用してログインします。これは今まで働いてきました。show メソッドでどのエラーが発生したかわかりません。

ログイン用の URL は 、「home#index」に対応する に移動することになっているログイン時のhttp://localhost:3000/login
URL に表示されるエラーです 。http://localhost:3000/sessions
http://localhost:3000

ホームコントローラー

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 ベイツ controller_authetic モジュール

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'
4

2 に答える 2

0

「current_user」変数をどのように取得したか教えてください。これには何か問題があると思います。ログインに成功した後にユーザーを保存する行がコードにありません。

于 2012-12-07T10:25:14.020 に答える
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
于 2013-05-26T14:19:32.070 に答える