简体   繁体   English

在相关表中存储current_user(设计)?

[英]store current_user (devise) in related table?

I use devise for our user management. 我将devise用于我们的用户管理。

models: 楷模:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name

  has_many :carts   

end

class Cart < ActiveRecord::Base
  has_many :line_items, dependent: :destroy
  accepts_nested_attributes_for :line_items
  belongs_to :user

end

cart_controller: cart_controller:

def create
    @cart = current_user.cart.new(cart_params)

    respond_to do |format|
      if @cart.save
        format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
        format.json { render action: 'show', status: :created, location: @cart }
      else
        format.html { render action: 'new' }
        format.json { render json: @cart.errors, status: :unprocessable_entity }
      end
    end
  end

private

    def cart_params
      params[:cart]
    end

module: 模块:

module CurrentCart
  extend ActiveSupport::Concern

  private

    def set_cart 
      @cart = Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      @cart = Cart.create
      session[:cart_id] = @cart.id 
    end
end

When a cart is created (when a list item is added) i want to store in user.id in cart table (user_id). 创建购物车时(添加列表项时),我想存储在购物车表(user_id)的user.id中。 So i thought to use the current_user method like this 所以我想使用current_user这样的方法

  @cart = current_user.cart.new(cart_params)

The cart is created with the items but the user_id in the cart table remains empty. 使用项目创建购物车,但购物车表中的user_id保持为空。 What am i doing wrong? 我究竟做错了什么?

thanks..remco 谢谢..remco

尝试

@cart = current_user.carts.build(cart_params)

As Matt Gibson pointed out(and that's how I would do to create new cart from an association): 正如马特·吉布森(Matt Gibson)所指出的(这就是我从关联创建新购物车的方式):

@cart = current_user.carts.build(cart_params)

But, I think it is not working as expect because of Rails 4's strong parameters. 但是,由于Rails 4的强大参数,我认为它无法按预期工作。 For that you might want to change this: 为此,您可能需要更改此设置:

private

    def cart_params
      params[:cart]
    end

to: 至:

private

    def cart_params
      params.require(:cart).permit(:name, :state) # at :name, :state use attributes which you're getting from the form for cart!
    end

Change 更改

def set_cart 
  @cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
  @cart = Cart.create
  session[:cart_id] = @cart.id 
end

to: 至:

def set_cart 
  @cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
  @cart = current_user.carts.create
  session[:cart_id] = @cart.id # since you're setting this ID in session which you use for update later!
end

It should be 它应该是

@cart = current_user.carts.new(cart_params)

As it is has_many relation you have to use carts instead of cart 由于它具有has_many关系,因此您必须使用购物carts而不是购物车

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM