1

更新: Ruby 1.8 で実行しています。リポジトリへのリンクは github.com/lauherk/sample_app です

Micheal hartl による Ruby on Rails チュートリアルを進めています。第 9 章では、サンプル ユーザーhttp://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersをデータベースに入力する際に​​問題が発生しています。 ?version=3.2#sec:sample_users

私のレーキのコードは次のとおりです。

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    User.create!(:name => "Example User",
                 :email => "example@railstutorial.org",
                 :password => "foobar",
                 :password_confirmation => "foobar")
    99.times do |n|
      name  = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password  = "password"
      User.create!(:name => name,
                   :email => email,
                   :password => password,
                   :password_confirmation => password)
    end
  end
end

両方を実行した後:

bundle exec rake db:reset
bundle exec rake db:populate

コマンドラインから次のエラーが表示されます。

rake aborted!
Can't mass-assign protected attributes: Lawrence Kertzmann
/Library/Ruby/Gems/1.8/gems/activemodel-3.2.2/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'

(この特定の名前は、レーキを実行するたびに変わります)

ユーザーモデルにコードがあることを確認しました

attr_accessible :name, :email, :password, :password_confirmation

設定も試しました

  config.active_record.whitelist_attributes = false

それでも、コマンドラインで同じ結果が得られます。

データベースにサンプル ユーザーを入力するためのヒントはありますか?

どうもありがとう。

4

2 に答える 2

1

追加

attr_accessible :name

モデルに。現在、:user_name(およびその他の値) のみがアクセス可能です。

于 2012-04-20T04:13:05.207 に答える
0

authentication_token を attr_accessible に追加するだけです。

Media1s-Mac-mini:rails_apps media1$ rake clipsfree_import RAILS_ENV=development csvfile=/Users/media1/Desktop/clips/atemp5/demotracks2/import.csv レーキが中止されました! 保護された属性を一括割り当てできません: title /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:48:in process_removed_attributes' /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:20:indebug_protected_attribute_removal' /usr/local/rvm/gems/ruby- /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel- 3.2.11/lib/active_model/mass_assignment_security.rb:230:insanitize_for_mass_assignment' /usr/local/rvm/gems/ruby-1.9.

解決策: models フォルダの loopsfree.rb の attr_accessible に属性 title を追加しました

class Loopsfree < ActiveRecord::Base attr_accessible :ISRC, :title, :artist, :bpm, :file_name, :genre, :id, :sub_genre end

これが役立つことを願っています:)ハッピーコーディング

于 2013-03-09T02:42:47.453 に答える