简体   繁体   中英

store current_user (devise) in related table?

I use devise for our user management.

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:

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). So i thought to use the current_user method like this

  @cart = current_user.cart.new(cart_params)

The cart is created with the items but the user_id in the cart table remains empty. What am i doing wrong?

thanks..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):

@cart = current_user.carts.build(cart_params)

But, I think it is not working as expect because of Rails 4's strong parameters. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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