Rails6.0.0 + Faker + Devise +种子

我使用gemfile devise在Rails上进行了登录功能。 然后,我尝试使用gemfile伪造器在seed.rb中创建伪数据,但出现以下错误消息。 请告诉我如何解决。

Rails 6.0.0 设计4.7.1 造假者2.7.0

obj->flat<Type>().data()
#seeds.rb

50.times do
  user=User.new(
    name: Faker::Internet.user_name,email: Faker::Internet.email,password: Faker::Internet.password
  )
end
user.save!

users = User.order(:created_at).take(6)
20.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.diaries.create!(content: body) }
end

@RomanAlekseiev @Sravan 非常感谢

我更改了代码和零件更正。 但是相反,我收到以下错误消息。 请告诉我如何解决。

#error message(terminal)

rails aborted!
NameError: undefined local variable or method `user' for main:Object
Did you mean?  users
/Users/mypc/study/diaryapp/db/seeds.rb:21:in `<main>'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:556:in `block in load_seed'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:676:in `with_inline_jobs'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:556:in `load_seed'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:440:in `load_seed'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/railties/databases.rake:328:in `block (2 levels) in <main>'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/command.rb:48:in `invoke'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/commands.rb:18:in `<main>'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
/Users/mypc/study/diaryapp/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
#seeds.rb

50.times do
  user=User.new(
    name: Faker::Internet.user_name,password: Faker::Internet.password
  )
  user.save!
end


users = User.order(:created_at).take(6)
20.times do
  content = Faker::Lorem.sentence(word_count:15)
  users.each { |user| user.diaries.create!(content: body) }
end
#error message(terminal)
rails aborted!
NoMethodError: undefined method `diaries' for #<User:0x00007f9e6c78b358>
~~~~
#app>controllers>diaries_controller.rb

class DiariesController < ApplicationController
  before_action :authenticate_user!,except: [:show]

  def index
    @diaries = Diary.all
  end

  def new
    @diary = Diary.new
  end

  def create
    @diary = current_user.diaries.build(diary_params)
    if @diary.save
        logger.debug "diary: #{@diary.attributes.inspect}"
        redirect_to diary_url,notice: "save"
    else
        render :new
    end
  end

  def edit
  end

  def update
    @diary.update!(diary_params)
    redirect_to diary_url,notice: "update"
  end

  def destroy
    @diary.destroy
    redirect_to diaries_url,notice: "delete"
  end

  private

    def diary_params
        params.require(:diary).permit(:name,:description)
    end

    def set_diary
        @diary = current_user.diaries.find(params[:id])
    end
end

#app>models>diary.rb

class Diary < ApplicationRecord
    belongs_to :user
    default_scope -> { order(created_at: :desc) }
    validates :user_id,presence: true
    validates :content,presence: true,length: { maximum: 140 }
end
xiejy1 回答:Rails6.0.0 + Faker + Devise +种子

在块user中无法访问您的变量50.times do end。将保存用户行放在块内,应该没问题

,

您必须将代码更改为:

seeds.rb

50.times do
  user=User.new(
    name: Faker::Internet.user_name,email: Faker::Internet.email,password: Faker::Internet.password
  )
  user.save!
end


users = User.order(:created_at).take(6)
20.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.diaries.create!(content: body) }
end

您试图在user.save!保存用户every loop超出了循环范围,也可以称为block,因此将这段代码移动到块中。

本文链接:https://www.f2er.com/3160803.html

大家都在问