样本帐户的种子

时间:2019-01-23 12:51:23

标签: ruby-on-rails ruby

我已经按照Hartl的教程制作了带有标记系统的ToDoList,并且还借助了word guidevideo。但是我不明白在User实例中用于初始化标签的实际参数是什么。根据我编写的代码创建示例User帐户的正确方法是什么,您通常如何确定这些方法的实际调用方式?

这是实现本教程中讲解的内容后的代码,如果需要更多信息,请告知我。

Micropost控制器

class MicropostsController < ApplicationController
  before_action :logged_in_user, only: [:index, :show, :create, :destroy]
  before_action :correct_user,   only: :destroy

  def index
    params[:tag] ? @microposts = Micropost.tagged_with(params[:tag]) : @microposts = Micropost.all
  end


  def show
    @micropost = Micropost.find(params[:id])
  end


  def create
    @micropost = current_user.microposts.build(micropost_params))
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    flash[:success] = "You have deleted a task!"
    redirect_to request.referrer || root_url
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content, :tag_list, :tag, 
        {tag_ids: [] }, :tag_ids)
    end

    def correct_user
      @micropost = current_user.microposts.find_by(id: params[:id])
      redirect_to root_url if @micropost.nil?
    end
end

Micropost模型

class Micropost < ApplicationRecord
  belongs_to :user
  has_many :taggings
  has_many :tags, through: :taggings, :dependent => :delete_all
  default_scope -> { order(created_at: :desc) }
  validates :user_id, presence: true
  validates :content, presence: true, length: {maximum: 140 }
  attr_accessor :tag_list



  def self.tagged_with(name)
    Tag.find_by!(name: name).microposts
  end

  def self.tag_counts
    Tag.select('tags.*, count(taggings.tag_id) as count')
    .joins(:taggings).group('taggings.tag_id')
  end

  def tag_list
    tags.map(&:name).join(', ')
  end

  def tag_list=(names)
    self.tags = names.split(',').map do |n|
      Tag.where(name: n.strip).first_or_create!
    end
  end
end

我亲自尝试过,但是由于不了解代码的工作原理,我的耙子中止了。

10.times do
    content = Faker::Lorem.sentence(5)
    users.each do |user|
      tag_list = Faker::Lorem.words(3)
      user.microposts.create(content: content, tag_list: tag_list)
    end
end

rails db:migrate:reset之后,再从rails db:seed之后,终端发出的错误消息

/usr/local/lib/ruby/gems/2.5.0/gems/bootstrap-4.0.0.alpha6/lib/bootstrap/version.rb:2: warning: already initialized constant Bootstrap::VERSION
/usr/local/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/lib/bootstrap-sass/version.rb:2: warning: previous definition of VERSION was here
/usr/local/lib/ruby/gems/2.5.0/gems/bootstrap-4.0.0.alpha6/lib/bootstrap/version.rb:3: warning: already initialized constant Bootstrap::BOOTSTRAP_SHA
/usr/local/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/lib/bootstrap-sass/version.rb:3: warning: previous definition of BOOTSTRAP_SHA was here
rails aborted!
NoMethodError: undefined method `strip' for ["aut", "rerum", "magni"]:Array
/Users/isaac/Documents/CVWO/cvwo_assignment/app/models/micropost.rb:27:in `block in tag_list='
/Users/isaac/Documents/CVWO/cvwo_assignment/app/models/micropost.rb:26:in `map'
/Users/isaac/Documents/CVWO/cvwo_assignment/app/models/micropost.rb:26:in `tag_list='
/usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_assignment.rb:46:in `public_send'
/usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_assignment.rb:46:in `_assign_attribute'
/usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_assignment.rb:40:in `block in _assign_attributes'
/usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_assignment.rb:39:in `each'
/usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_assignment.rb:39:in `_assign_attributes'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/attribute_assignment.rb:26:in `_assign_attributes'
/usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_assignment.rb:33:in `assign_attributes'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/core.rb:337:in `initialize'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/inheritance.rb:66:in `new'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/inheritance.rb:66:in `new'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/reflection.rb:154:in `build_association'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/association.rb:281:in `build_record'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/collection_association.rb:358:in `block in _create_record'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/collection_association.rb:129:in `block in transaction'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/transactions.rb:210:in `transaction'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/collection_association.rb:128:in `transaction'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/collection_association.rb:357:in `_create_record'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/has_many_association.rb:121:in `_create_record'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/association.rb:196:in `create'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/associations/collection_proxy.rb:347:in `create'
/Users/isaac/Documents/CVWO/cvwo_assignment/db/seeds.rb:27:in `block (2 levels) in <top (required)>'
/Users/isaac/Documents/CVWO/cvwo_assignment/db/seeds.rb:25:in `each'
/Users/isaac/Documents/CVWO/cvwo_assignment/db/seeds.rb:25:in `block in <top (required)>'
/Users/isaac/Documents/CVWO/cvwo_assignment/db/seeds.rb:23:in `times'
/Users/isaac/Documents/CVWO/cvwo_assignment/db/seeds.rb:23:in `<top (required)>'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `block in load'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/engine.rb:549:in `load_seed'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:270:in `load_seed'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:184:in `block (2 levels) in <top (required)>'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/command.rb:46:in `invoke'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/Users/isaac/Documents/CVWO/cvwo_assignment/bin/rails:9:in `require'
/Users/isaac/Documents/CVWO/cvwo_assignment/bin/rails:9:in `<top (required)>'
/usr/local/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/usr/local/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/usr/local/lib/ruby/gems/2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/usr/local/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/usr/local/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/isaac/Documents/CVWO/cvwo_assignment/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

1 个答案:

答案 0 :(得分:1)

此功能:

  def tag_list=(names)
    self.tags = names.split(',').map do |n|
      Tag.where(name: n.strip).first_or_create!
    end
  end

期望一个字符串,例如“ foo,bar,baz”。

但是,此测试代码:

  tag_list = Faker::Lorem.words(3)
  user.microposts.create(content: content, tag_list: tag_list)

正在传递数组,例如[“ foo”,“ bar”,“ baz”]

您可以使用#join将数组转换为字符串,如下所示:

  tag_list = Faker::Lorem.words(3).join(",")
  user.microposts.create(content: content, tag_list: tag_list)