@mymodel.user = current_user 不适合我

发布于 2024-11-15 20:24:52 字数 1972 浏览 1 评论 0原文

我遵循了这些问题(一个两个)如何调整我的方法仅限当前用户,以便他们拥有特定的 user_id 来编辑、更新、创建和销毁其产品。

这是我的代码:

我的迁移:

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.string :name
      t.date :date
      t.decimal  :price, :default => 0, :precision => 10, :scale => 2
      t.integer :user_id
      t.float :latitude
      t.float :longitude

控制器:

class ProductsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @products = Product.all

  def show
    @product = Product.find(params[:id])

  def new
   @product = Product.new

  def edit
    @product.user = current_user
    @product = Product.find(params[:id])
  end

  def create
    @product.user = current_user
    @product = Product.new(params[:product])

  def update
    @product.user = current_user
    @product = Product.find(params[:id])


  def destroy
    @product.user = current_user
    @product = Product.find(params[:id])
    @product.destroy

产品模型:

class Product < ActiveRecord::Base
    attr_accessible :name, :date, :price, :tag_list 
    belongs_to :user
end

然后设计用户模型:

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me 

  has_many :products,        :dependent => :destroy

我尝试提交它,然后突然弹出:

ProductsController#create 中的NoMethodError

undefined method `user=' for nil:NilClass

我是什么遗漏或做错了?

提前致谢!

I followed these questions(one and two) on how to adjust my methods with the current user only so they have the specific user_id to edit,update, create and destroy their products.

Here is my code:

My Migration:

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.string :name
      t.date :date
      t.decimal  :price, :default => 0, :precision => 10, :scale => 2
      t.integer :user_id
      t.float :latitude
      t.float :longitude

the Controller:

class ProductsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @products = Product.all

  def show
    @product = Product.find(params[:id])

  def new
   @product = Product.new

  def edit
    @product.user = current_user
    @product = Product.find(params[:id])
  end

  def create
    @product.user = current_user
    @product = Product.new(params[:product])

  def update
    @product.user = current_user
    @product = Product.find(params[:id])


  def destroy
    @product.user = current_user
    @product = Product.find(params[:id])
    @product.destroy

Product Model:

class Product < ActiveRecord::Base
    attr_accessible :name, :date, :price, :tag_list 
    belongs_to :user
end

then the Devise User Model:

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me 

  has_many :products,        :dependent => :destroy

I try to submit it and then suddenly this pops up:

NoMethodError in ProductsController#create

undefined method `user=' for nil:NilClass

What am i missing or doing wrong?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-11-22 20:24:52

您尝试在拥有产品之前分配@product.user。首先在产品上查找,然后分配用户。

@product = Product.find(params[:id])
@product.user = current_user

对于除 create 以外的操作,您可以执行类似以下操作(假设您在 User 模型中设置了 has_many :products 关联这

@product = current_user.products.find(params[:id])

将限制查找到 user_id 等于 current_user 的产品。

You are trying to assign @product.user before you have a product. Do your find on the product first, then assign the user.

@product = Product.find(params[:id])
@product.user = current_user

For the actions other than create where you want to restrict the product to the current_user, you can do something like this (assuming you setup the has_many :products association in your User model.

@product = current_user.products.find(params[:id])

That will restrict find to just those products that have the user_id equal to the current_user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文