我在 Twitter 克隆上的 Charles Max wood 教程上。我收到此错误RuntimeError in HomeController#show
undefined method `all_flits' for nil:NilClass
但是当我在控制台上尝试时,该方法有效
与 raise session.inspect 我得到
{"return_to"=>"http://localhost:3000/sessions", "session_id"=>"2baca34a681d6a9e259991481891f7bf", "_csrf_token"=>"pKRBBE3m4+T+aTeBBFvqDsEsKxPaWG5AxF3hBiWlV2E="}
我使用 Ryan Bates 漂亮的生成器登录。一直工作到现在。我不知道我在 show 方法中遇到了哪个错误。
登录的 URL 是 http://localhost:3000/login
我得到的错误是 http://localhost:3000/sessions
在登录时的 URL 我应该去http://localhost:3000
对应于 'home#index'
家庭控制器
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
显示.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 }%>
模型用户.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 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'