Ruby on rails 在创建行项目记录时将会话用户Id添加到行项目表中-Ruby on Rails

Ruby on rails 在创建行项目记录时将会话用户Id添加到行项目表中-Ruby on Rails,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,rails-activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,Rails Activerecord,我正在尝试将用户会话ID添加到第_行项目表中。我到处都在研究,找不到答案,所以我不得不把它贴在这里。此应用程序成功地将电话ID和购物车ID添加到Line_Items表中,但在我尝试电话和购物车ID的类似方法时,不允许我添加用户会话ID。有关我当前的实现,请参阅以下内容: 行项目控制器: def create # current_cart method in application_controller.rb @cart = current_cart phone = Phone.fin

我正在尝试将用户会话ID添加到第_行项目表中。我到处都在研究,找不到答案,所以我不得不把它贴在这里。此应用程序成功地将电话ID和购物车ID添加到Line_Items表中,但在我尝试电话和购物车ID的类似方法时,不允许我添加用户会话ID。有关我当前的实现,请参阅以下内容:

行项目控制器:

def create
  # current_cart method in application_controller.rb
  @cart = current_cart
  phone = Phone.find(params[:phone_id])
  if phone.stock > 0
    @line_item = @cart.add_phone(phone.id )
    if @line_item.save
      redirect_to  :back, notice: " '#{phone.model}'  has been added to your cart."
    else
      render action: "new" 
    end
  else
    redirect_to  :back, notice: " Cannot add: '#{phone.model}' - stock level is too low."
  end
end
Cart.rb型号

def add_phone(phone_id)
  current_item = line_items.find_by_phone_id(phone_id)
  if current_item
    current_item.quantity += 1
  else
    current_item = line_items.build(phone_id: phone_id)
    current_item.quantity = 1
  end
  current_item.phone.stock -= 1
  current_item.phone.save
  current_item
end

# returns true if stock level is greater than zero
def can_add(phone)
  phone.stock > 0
end
如果需要进一步的代码,请告诉我。

保存

无法插入用户ID的原因可能有很多:

如果您使用的是实例方法(如
Cart.rb
)和Desive之类的工具,则可能无法直接访问
当前用户
助手;您必须通过以下方法传递它:

def add_user(user_id)
    @user = User.find(user_id)
    #etc
end
如果您直接发布与用户对象相关的错误或其他代码,我们将更好地提供帮助


重构

我认为您将受益于以下方法(&T):


问题是Rails中的模型类对控制器和会话一无所知。我假设您正在使用某种身份验证机制,该机制为您提供会话中的当前用户。。。因此,我们必须将其纳入您的add_phone方法中

在控制器中,我将更改这一行:


@行\u项=@cart.add\u电话(phone.id)

为此:


@行\u项=@cart.add\u电话(电话,当前用户)

(注意这里有两个更改-一个是传递电话而不是它的id,因为您已经找到了它,另一个是传递当前用户

然后,我们要将您的add_phone方法更改为如下所示:

def add_phone(phone, current_user = nil)
  # make sure your Item class initializes quantity to o, not nil.
  current_item = line_items.find_or_initialize_by_phone_id(phone.id)

  current_item.increment!(:quantity)
  current_item.phone.decrement!(:stock)
  current_item.user = current_user
  current_item.phone.save
  current_item
end
请注意,我将用户默认设置为nil…这样,您已经拥有的不提供该字段的代码将继续工作。我还使用find_或_initialize_by helper稍微简化了您的方法…避免了“if”测试。此外,递增和递减helper稍微清理了代码的意图

您应该确保行项目类包括


属于:用户


如果您发现自己处于需要了解当前用户以获取大量域逻辑的情况下,您可能需要查看Senentint用户gem。

是否有任何错误?表中是否有用户会话id列?失败的代码看起来如何?在line\u items表中,我将用户id作为外键来存储用户会话id whic当用户单击“添加”按钮时,必须将用户会话插入该列。我尝试了@line\u item=@user.add\u user(user.id)在if phone.stock>0但收到未定义的局部变量或方法“user”for#下,我想我不知道如何将用户ID放入第_itemsuser_ID列电话和用户之间的关系是什么?是用户有_多部电话吗?我没有电话和用户之间的任何关系,但有用户和第_I行之间的关系作为用户的tem有很多:line\u Items超过一百万,当我添加了我没有的当前用户方法时,这非常有效。但是我有另一个问题。用户id 2为产品1添加line\u item,当用户id 4添加相同的产品时,表中的用户id更改为4,数量增加了一个。我如何解决这个问题?我如何解决如果我使用一种方法在用户注销时销毁cart和line_项目?我现在感到困惑。我实际上是一名大学生,与我的团队一起在这个项目中工作。你认为将用户ID插入Carts表而不是line items表更好吗?
def add_phone(phone_id)
  current_item = line_items.find_by_phone_id(phone_id)
  if current_item
    current_item.incremenet!(:quality)
  else
    current_item = line_items.build(phone_id: phone_id)
    current_item.quantity = 1
  end
  current_item.phone.decrement!(:stock)
  current_item.phone.save
  current_item
end
def add_phone(phone, current_user = nil)
  # make sure your Item class initializes quantity to o, not nil.
  current_item = line_items.find_or_initialize_by_phone_id(phone.id)

  current_item.increment!(:quantity)
  current_item.phone.decrement!(:stock)
  current_item.user = current_user
  current_item.phone.save
  current_item
end