whats wrong with my collection_select in my sign u

2019-08-11 07:02发布

问题:

I am newbie to rails ,

I am using devise from ryan bates video tutorial , and , i am stuck at one point

I have created user , role relationship

and in the sign up page , i need to provide select option group for existing roles ,

in my sign up view page i am writing

<%= collection_select(:user,:roles,Role.find(:all),:id,:name) %>

i dont precisely understand collection_select method , kindly help what i might be doing wrong

my models 1 : user.rb

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me , :roles

  has_and_belongs_to_many :roles

  def role?(role)
    return !!self.roles.find_by_name(role.to_s.camelize)
  end
end

my model 2 : role.rb

class Role < ActiveRecord::Base
  attr_accessible :name
  has_and_belongs_to_many :users
end

my user migration file

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
  end
end

my role migration file

class CreateRoles < ActiveRecord::Migration
  def change
    create_table :roles do |t|
      t.string :name

      t.timestamps
    end
  end
end

my join table migration file

class UsersHaveAndBelongToManyRoles < ActiveRecord::Migration
  def up
    create_table :roles_users, :id => false do |t|
      t.references :role, :user
    end
  end

  def down
    drop_table :roles_users
  end
end

the ERROR COMING is undefined method `each' for "2":String

2 being the id of the role selected

回答1:

In a has_and_belongs_to_many relationship like you have between User and Role, there is no role_id on the User object.

The second parameter of the collection_select is the attribute you're updating with the selection(s), in your case it's not role_id, it's role_ids and seeing as it's a has_and_belongs_to_many relationship you probably want to allow the user to select multiple options, so try something like this:

<%= collection_select(:user, :role_ids, Role.all, :id, :name, {}, { selected: @user.role_ids, multiple: true }) %>

If you attach it to a form_for on your @user object you can use:

<%= f.collection_select(:role_ids, Role.all, :id, :name, {}, multiple: true) %>