@mymodel.user = current_user 不适合我
我遵循了这些问题(一个和两个)如何调整我的方法仅限当前用户,以便他们拥有特定的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试在拥有产品之前分配@product.user。首先在产品上查找,然后分配用户。
对于除 create 以外的操作,您可以执行类似以下操作(假设您在
User
模型中设置了has_many :products
关联这将限制查找到 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.
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 yourUser
model.That will restrict find to just those products that have the user_id equal to the current_user.