Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 错误self.user.update_属性未定义方法nil:Class_Ruby On Rails_Ruby_Ruby On Rails 3_Rspec_Capybara - Fatal编程技术网

Ruby on rails 错误self.user.update_属性未定义方法nil:Class

Ruby on rails 错误self.user.update_属性未定义方法nil:Class,ruby-on-rails,ruby,ruby-on-rails-3,rspec,capybara,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,Capybara,更新帖子我是RoR中的noob,我开始测试。我有一个应用程序,我尝试使用rspec和capybara测试,我想创建用户并测试登录。但当我进行测试时,我的模型用户出现了一些错误,因为在我的应用程序中,我创建了用户,并调用了after_create:create_顺序 我修改了我的factories.rb,但我的update\u属性中有一个错误 请参阅my model user.rb class User < ActiveRecord::Base # Include default

更新帖子我是RoR中的noob,我开始测试。我有一个应用程序,我尝试使用rspec和capybara测试,我想创建用户并测试登录。但当我进行测试时,我的模型用户出现了一些错误,因为在我的应用程序中,我创建了用户,并调用了after_create:create_顺序

我修改了我的factories.rb,但我的update\u属性中有一个错误

请参阅my model user.rb

    class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  has_many :ratings
  has_many :rated_sounds, :through => :ratings, :source => :sounds
  has_many :sounds ,:dependent => :destroy
  has_many :orders ,:dependent => :destroy
  has_many :song_ups ,:dependent => :destroy
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :registerable, :confirmable
  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :nom, :prenom, :societe, :tel, :cgu, :sign_in_count, :trans_simu,
            :trans_limit, :project, :vat_number, :adress, :zip_code, :city, :tutorial

  after_create :create_order

 def create_order
  order = Order.create(user_id: self.id,files_left: 3)
  order.subscription =  Subscription.where(category: 'test').first || Subscription.create(category: 'test')
  self.update_attribute(:trans_limit, 1)

  #Ancien Order
 # Order.create(user_id: self.id, subscription_id: Subscription.where(category: 'test').first.id, files_left: 3)
  # self.update_attribute(:trans_limit, 1)
end

  def test?
    self.orders.trial.present? && self.orders.count==1
  end

  def unlimited?
    !self.test? && self.orders.current.where(time_left: -1).any?
  end

  def allow_send?
    !self.finish_order? && self.sounds.in_progress.count < self.trans_limit.to_i
  end

  def finish_order?
    self.orders.current.empty?
  end
end
我的一个测试是:

scenario "User login right" do
    visit new_user_session_path
    current_path.should == "/users/sign_in"
    page.html.should include('<h2>Se connecter</h2>')
    user = FactoryGirl.create(:user)
    fill_in "Email", :with => user.email
    fill_in "Mot de passe", :with => user.password
    check "user_remember_me"
    click_button "Connexion"
    page.should have_content('Mon compte')
    current_path.should == root_path
  end

我希望你能帮助我。感谢的

创建订单
中尝试使用以下方法:

Order.create!(user: self, subscription: Subscription.where(category: 'test').first, files_left: 3)
使用对象而不是普通ID

在方法之前
要确保验证通过,请在这些方法的末尾返回
true
。在您的情况下,在
init_value
方法的末尾添加
return true

您的数据库中没有类别为“test”的订阅。解决方案取决于您希望如何处理此类错误

如果希望此订阅始终位于数据库中,请使用db:seed-rake任务预填充数据库。(试着用谷歌搜索一下,看看怎么做)

如果给定的订阅不存在,则不想分配任何订阅,请尝试:

def create_order
  order = Order.create(user_id: self.id,files_left: 3)
  order.subscription =  Subscription.where(category: 'test').first
  self.update_attribute(:trans_limit, 1)
end
最后,如果您想要创建这样一个订阅(如果它不存在):

def create_order
  order = Order.create(user_id: self.id,files_left: 3)
  order.subscription =  Subscription.find_or_create_by_category('test')
  self.update_attribute(:trans_limit, 1)
end     

我尝试了您编写的内容,但我遇到了另一个错误:ActiveModel::MassAssignmentSecurity::error:无法批量分配受保护的属性:用户、订阅#./app/models/user。rb:21:在“创建订单”中,确保
用户
订阅
字段在
订单
模型中可用。例如,验证是否在订单中设置了
所属:用户
所属:订阅
我的订单模型中有所属:订阅和所属:用户,所属:用户所属:订阅。如果你愿意,我可以上传我的订单型号。我已经更新了答案。可能是某个方法中缺少
返回true
的问题。好的,我尝试了您的第二个选择,我理解我的问题,但现在我在测试中遇到了另一个问题:1)UserAction用户登录权限失败/错误:User=FactoryGirl.create(:User)NoMethodError:nil:NilClass./app/models/order的未定义方法
trans_seconds'.\rb:13:in
init_value'./app/models/user.rb:21:in
create_order'./spec/features/user_connection_spec.rb:39:in
block(2级)在'I Upload my order.rb'中,每当您看到错误
'foo'未定义为nil:NilClass
时,这意味着您在不期望的地方得到了nil。在本例中,在init_value方法的第一行中,self.subscription是nil,因为它不是由您的工厂创建的。好的,那么我需要在我的用户工厂中添加订阅吗?像这样:订阅“测试”?或者不,我需要创建新工厂?因为订阅用于创建订单,订单用于用户。您应该在此处阅读有关工厂的信息:。关于创建关联对象有一个非常好的章节。我编辑了我的帖子,但是我的update_属性有错误,我不知道为什么。你能帮忙吗?
Order.create!(user: self, subscription: Subscription.where(category: 'test').first, files_left: 3)
def create_order
  order = Order.create(user_id: self.id,files_left: 3)
  order.subscription =  Subscription.where(category: 'test').first
  self.update_attribute(:trans_limit, 1)
end
def create_order
  order = Order.create(user_id: self.id,files_left: 3)
  order.subscription =  Subscription.find_or_create_by_category('test')
  self.update_attribute(:trans_limit, 1)
end