我用gemfile devise在Rails上做了一个Login函数。然后我试着用gemfile faker在seeds.rb中做虚拟数据,但是我得到了下面的错误信息。请告诉我如何解决。
导轨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@斯拉万非常感谢
我改变了代码和部分更正。但相反,我得到了以下错误信息。请告诉我如何解决。
#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
2条答案
按热度按时间n6lpvg4x1#
您必须将代码更改为:
种子.rb
您试图在
every loop
中保存用户的user.save!
超出了循环/它也可以称为block
,因此必须将该代码段移到块内。nsc4cvqm2#
您的变量
user
无法从块50.times do end
中访问。请将保存user行放入块中,这样就可以了