0

I am new to Ruby on Rails. I have a migration called create user

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.column :username, :string,        :limit => 25, :default => "", :null => false
      t.column :hashed_password, :string, :limit => 40, :default => "", :null => false
      t.column :first_name, :string,      :limit => 25, :default => "", :null => false
      t.column :last_name, :string,       :limit => 40, :default => "", :null => false
      t.column :email, :string,           :limit => 50, :default => "", :null => false
      t.column :display_name, :string,    :limit => 25, :default => "", :null => false
      t.column :user_level, :integer,     :limit => 3,  :default => 0,  :null => false
    end

 User.create(:username=>'test',:hashed_password=>'test',:first_name=>'test',:last_name=>'test',:email=>'test@test.com',:display_name=> 'test',:user_level=>9)  
  end
end

When I run rake db:migrate the table is created with the columns as mentioned above but the test data are not there

mysql>select * from users;
Empty set (0.00 sec)

EDIT I just dropped the whole database and restarted the migration and now it is showing the following error.

rake aborted!
An error has occurred, all later migrations canceled:

Can't mass-assign protected attributes: username, hashed_password, first_name, last_name, email, display_name, user_level

What am I doing wrong please help? Thank you.

4

3 回答 3

1

add

attr_accessible :username, :hashed_password, :first_name, :last_name, :email, :display_name, :user_level

to your user.rb

于 2013-11-04T10:07:51.347 回答
0

That's Rails way of prohibiting users to create or update objects via a param hash. You need to specify your User attributes as attr_accessible in your model:

example :

class User
 attr_accessible :username, :firstname (etc)
end

Read more about Mass Assignment here.

于 2013-11-04T10:01:22.327 回答
0

just to complete the answer about testing environment. You can run rake db:test:prepare to check the migrations and load schema !

于 2013-11-04T10:59:46.550 回答