@ mymodel.user = current_user不适合我 [英] @mymodel.user = current_user is not working for me
问题描述
这是我的代码:
我的迁移:
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:纬度
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
产品型号:
ActiveRecord :: Base
attr_accessible:name,:date,:price,:tag_list
belongs_to:user
end
然后Devise用户模型:
class User< ActiveRecord :: Base
devise:database_authenticatable,:可注册,
:可恢复,可记忆,可追溯,可验证
attr_accessible:电子邮件,密码,密码确认,:remember_me
has_many:products,:dependent => :销毁
我尝试提交,然后突然出现:
ProductController中的NoMethodError#create
未定义的方法`user =对于nil:NilClass
我缺少或做错了什么?
提前感谢
您正在尝试分配@product。使用者在拥有产品之前。首先找到产品,然后分配给用户。
@product = Product.find(params [:id])
@ product.user = current_user
除了创建要限制的位置以外的操作产品到current_user,你可以这样做(假设你在用户
中设置 has_many:products
关联模型。
@product = current_user.products.find(params [:id])
这将限制只有那些具有user_id等于current_user的产品。
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!
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.
这篇关于@ mymodel.user = current_user不适合我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!