我尝试使用railsstutorial rails4.0
现在9.2.0并且没有Rspec错误。
但是{/ 1}}在打开/用户
时,用户#index中有NoMethodError为什么?
app / models / user.rb
rails s
app / controllers / users_controller.rb
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end
app / views / users / index.html.slim
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
end
def update
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
#Before actions
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
end
app / helpers / users_helper.rb
- provide(:title, 'All users')
h1 All users
ul.users
- @users.each do |user|
li
= gravatar_for user, size: 52
= link_to user.name, user
答案 0 :(得分:2)
我没试过。但它可能是这样的。
before_save { self.email = email.downcase if email}
答案 1 :(得分:0)
您的错误似乎来自调用gravatar_for
的{{1}}方法的第一行,建议您至少有一位用户在数据库中拥有user.email.downcase
个电子邮件地址