Rails:UsersController中的NoMethodError #create

时间:2013-02-27 18:56:24

标签: ruby-on-rails ruby ruby-on-rails-3.2

一切正常,然后我突然在创建新用户时遇到此错误。根据错误消息msg @user在create方法中为nil。我没有得到它,因为一切看起来都很好,如果我不能很快解决,我可能最终会永远发疯。我将非常感谢帮助..

Rails : NoMethodError in UsersController#create
undefined method `firstname' for nil:NilClass

我检查了一切,尝试了50次。仍然无法弄清楚出了什么问题。这是我的User.rb

class User < ActiveRecord::Base
attr_accessible  :firstname, :lastname, :password, :password_confirmation, :email,     :avatar
has_secure_password
has_attached_file :avatar, :styles => { medium: "300x300>", thumb: "40x40>", tiny: "28x28" }



EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :firstname, presence: true, length: {maximum: 50}
validates :lastname, presence: true, length: {maximum: 50}
validates :password, presence: true, length: {minimum: 6}
validates :password_confirmation, presence: true
validates :email, presence: true, format: {with: EMAIL_REGEX}, uniqueness: { case_sensitive: false } 


before_save :create_cookie

before_save(on: :create) do
  self.ip = request.remote_ip
end

private 

def create_cookie
  self.remember_cookie = SecureRandom.urlsafe_base64
end

end

在userscontroller.rb

def new
  if signed_in?
    redirect_to root_path
  else
    @user = User.new
  end
end

def create

@user = User.new(params[:user])
if @user.save
  sign_in @user
  redirect_to @user
else
  redirect_to signup_path
end

最后查看&gt;&gt; new.html.erb

<h1>Sign Up</h1>

 <%= form_for(@user) do |f| %>
  <%= f.label :firstname %>
      <%= f.text_field :firstname %>

  <%= f.label :lastname %>
  <%= f.text_field :lastname %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation %>

  <%= f.label :avatar, "Your Profile Picture"%>
  <%= f.file_field :avatar %>
  <%= f.submit "Create my account", class: "btn btn-primary" %>

 <% end %>

这里是development.log错误详情

 Started POST "/users" for 127.0.0.1 at 2013-02-27 20:40:10 +0200
 Processing by UsersController#create as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"PYJs3EKP+aqpwLwGOJKu46AATHNxxJ42eS4DS5KmolQ=", "user"=>{"firstname"=>"asish", "lastname"=>"bhattarai", "email"=>"getaasciesh@hotmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "avatar"=>#  <ActionDispatch::Http::UploadedFile:0x007f9684c3aec0 @original_filename="asishpic.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"asishpic.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130227-4600-18fct2a>>}, "commit"=>"Create my account"}

   [1m[36mUser Exists (0.2ms)[0m  [1mSELECT 1 AS one FROM "users" WHERE      LOWER("users"."email") = LOWER('getaasciesh@hotmail.com') LIMIT 1[0m

 [1m[35m (0.8ms)[0m  begin transaction
 [1m[36mCACHE (0.0ms)[0m  [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."email") =   LOWER('getaasciesh@hotmail.com') LIMIT 1[0m
  [1m[35mUser Load (1.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
 [1m[36m (0.2ms)[0m  [1mrollback transaction[0m
 Completed 500 Internal Server Error in 1707ms

 NoMethodError (undefined method `firstname' for nil:NilClass):
 app/models/user.rb:18:in `block in <class:User>'
 app/controllers/users_controller.rb:22:in `create'

此处还有会话助手,它提供了sign_in方法

 module SessionsHelper

def sign_in(user)
    cookies[:remember_cookie]=user.remember_cookie
    current_user= user
end

def current_user=(user)
 @current_user = user
end

def current_user
    @current_user ||= User.find_by_remember_cookie(cookies[:remember_cookie])
end


def signed_in?
    !current_user.nil?
  end

def current_user? (user)
    user == current_user
end
def sign_out 
    cookies.delete :remember_cookie
    current_user = null
end

def store_request_path
    session[:return_to] = request.fullpath
end

def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
end
end

0 个答案:

没有答案