Rails6.0.0 + Faker + Devise +种子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails6.0.0 + Faker + Devise +种子相关的知识,希望对你有一定的参考价值。
我使用gemfile devise在Rails上创建了登录功能。然后,我尝试使用gemfile伪造器在seed.rb中创建伪数据,但出现以下错误消息。请告诉我如何解决。
Rails 6.0.0设计4.7.1造假者2.7.0
#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
#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)
@ RomanAlekseiev@Sravan非常感谢]]
我更改了代码和零件更正。但是相反,我收到以下错误消息。请告诉我如何解决。
#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(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
#root Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / diaries#index diaries GET /diaries(.:format) diaries#index POST /diaries(.:format) diaries#create new_diary GET /diaries/new(.:format) diaries#new edit_diary GET /diaries/:id/edit(.:format) diaries#edit diary PATCH /diaries/:id(.:format) diaries#update PUT /diaries/:id(.:format) diaries#update DELETE /diaries/:id(.:format) diaries#destroy rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
我使用gemfile devise在Rails上创建了登录功能。然后,我尝试使用gemfile伪造器在seed.rb中创建伪数据,但出现以下错误消息。请告诉我如何解决。 ...
答案
您的变量user
在块50.times do end
中不可访问。将保存用户行放在块内,应该没问题
另一答案
您必须将代码更改为:
以上是关于Rails6.0.0 + Faker + Devise +种子的主要内容,如果未能解决你的问题,请参考以下文章
python ImportError:无法从'faker'导入名称'Faker'