未定义方法' scard'为nil:NilClass。

[英]undefined method `scard' for nil:NilClass


app/models/user.rb:51:in `cart_count'
app/views/layouts/_header.html.erb:24:in `block in     _app_views_layouts__header_html_erb__3312865121531214569_70179387150260'
app/views/layouts/_header.html.erb:23:in `_app_views_layouts__header_html_erb__3312865121531214569_70179387150260'
app/views/users/show.html.erb:40:in `_app_views_users_show_html_erb___592785710009336321_70179415740600'

When I attempt to view page after identification, I get the error above. Here's my layouts/header.html.erb code:

当我试图查看页面后,我得到了上面的错误。这是我的布局/ header.html。erb代码:

                            <% if signed_in? %>
                                <%= link_to accueilconnect_path do%>
                                    <li class="forma">Accueil</li>  
                                <% end %>
                                <%= link_to nosformations_path do%>
                                    <li class="contac">Formations</li>  
                                <% end %>
                                <%= link_to devenirformateur_path do%>
                                    <li class="contac">Devenir formateur</li>   
                                <% end %>
                                <%= link_to contacts_path do%>
                                    <li class="contac">Contact</li> 
                                <% end %>   
                                <%= link_to cart_path do%>
                                    <i class="fi-shopping-cart"></i> My Cart (<span class="cart-count"><%=current_user.cart_count%></span>)
                                <%end%>
                                <%= link_to image_tag('user2.png', :class => "user_icon"), current_user  %>
                            <% end %>

and here is my User Model :

这是我的用户模型:

class User < ActiveRecord::Base

类用户< ActiveRecord::Base

attr_accessor :password
before_save { self.email = email.downcase }

attr_accessible :name, :email, :login, :password, :password_confirmation

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

validates :name, :presence => true,
                 :length   => { :maximum => 50 }

validates :email, :presence => true,
                  :format   => { :with => email_regex },
                  :uniqueness => { :case_sensitive => false }

validates :login, :presence => true,
                  :length   => { :maximum => 20 }

validates :password, :presence     => true, 
                     :confirmation => true,
                     :length       => { :within => 6..40 } 

before_create :confirmation_token
before_create :encrypt_password 

      # Retour true (vrai) si le mot de passe correspond.
  def has_password?(password_soumis)
    encrypted_password == encrypt(password_soumis)
    # Compare encrypted_password avec la version cryptée de
    # password_soumis.
  end


    def authenticate(submitted_password)
      self.has_password?(submitted_password)
    end

  def self.authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt) ? user : nil
  end

    def email_activate
        self.email_confirmed = true
        self.confirm_token = nil
        save!(:validate => false)
    end

 def cart_count
     $redis.scard "cart#{id}"
 end

  private

    def encrypt_password
      self.salt = make_salt if new_record?
      self.encrypted_password = encrypt(password)
    end

    def encrypt(string)
      secure_hash("#{salt}--#{string}")
    end

    def make_salt
      secure_hash("#{Time.now.utc}--#{password}")
    end

    def secure_hash(string)
      Digest::SHA2.hexdigest(string)
    end

    def confirmation_token
      if self.confirm_token.blank?
      self.confirm_token = SecureRandom.urlsafe_base64.to_s
      end
    end

end

problem is in :

问题是:

def cart_count $redis.scard "cart#{id}" end

def cart_count复述,美元。scard“车# { id }”结束

any idea ?

任何想法?

1 个解决方案

#1


0  

Did you installed Redis Server:

你安装Redis服务器了吗?

http://redis.io/download

and Run it locallay go to where redis folder exist and write

然后运行locallay到redis文件夹的存在和写入。

redis-server

and define it in intalizers:

并在intalizer中定义它:

$redis = Redis.new
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/04/22/6f841f7bf87935f4dd2fc7edb5d4fa99.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告