Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从一个表中获取下拉列表中的值,并将其保存到另一个表中,以便在RubyonRails中进行多对多关联_Ruby On Rails_Ruby_Database - Fatal编程技术网

Ruby on rails 从一个表中获取下拉列表中的值,并将其保存到另一个表中,以便在RubyonRails中进行多对多关联

Ruby on rails 从一个表中获取下拉列表中的值,并将其保存到另一个表中,以便在RubyonRails中进行多对多关联,ruby-on-rails,ruby,database,Ruby On Rails,Ruby,Database,我有两个模型用户和角色,其中有第三个模型用户角色。我想在创建时为用户分配一些角色。一个用户可以有多个角色,一个角色可以分配给多个用户。我已经在角色表中创建了经理和管理员这样的角色。现在我需要在创建用户时在用户页面的下拉列表中显示这些保存的记录。用户和角色模型之间存在多对多关联 我所做的:我已将数据保存为角色表中的角色名称,如经理、hr或amdin Doubuts:现在我想在创建用户时在用户页面上的下拉列表中显示这些保存的记录,并希望与用户记录一起保存。我不知道如何在下拉列表中获取用户页面上的角色

我有两个模型用户和角色,其中有第三个模型用户角色。我想在创建时为用户分配一些角色。一个用户可以有多个角色,一个角色可以分配给多个用户。我已经在角色表中创建了经理和管理员这样的角色。现在我需要在创建用户时在用户页面的下拉列表中显示这些保存的记录。用户和角色模型之间存在多对多关联

我所做的:我已将数据保存为角色表中的角色名称,如经理、hr或amdin

Doubuts:现在我想在创建用户时在用户页面上的下拉列表中显示这些保存的记录,并希望与用户记录一起保存。我不知道如何在下拉列表中获取用户页面上的角色表数据,然后将其保存到角色表中

user.rb

class User < ActiveRecord::Base
 has_many :roles,  :through => :role_users
 has_many :roles_users
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email, :password, :password_confirmation, 
  :remember_me, :first_name, :last_name, :is_admin, :contact_no, :birth_date,
   :joining_date, :is_active, :is_hr, :is_manager, :user_code, :designation
  # attr_accessible :title, :body
end
  class Role < ActiveRecord::Base
      attr_accessible :name
      has_many :users,  :through => :role_users
      has_many :role_users
    end

role_user.rb



 class RolesUser < ActiveRecord::Base
      attr_accessible :role_id, :user_id
      belongs_to :user 
      belongs_to :role
    end
class RolesController < ApplicationController
  before_filter :authorize_admin!

    def index
        @roles = Role.all
    end

    def new
     @role = Role.new 
    end

    def create
      @role  = Role.new(params[:role])
         if @role.save
           flash[:success] = "role created!"
           redirect_to role_path(@role)
       else
            render 'new' 
        end 
    end

    def show
      @role = Role.find(params[:id])
    end

    def edit
      @role = Role.find(params[:id])
    end

    def update
      @role = Role.find(params[:id])
    if @role.update_attributes(params[:role])
        flash.notice = "Role #{@role.name} has been updated"
        redirect_to role_path(@role)
    else 
       render 'edit'
    end
   end 

    def destroy
      @role = Role.find(params[:id])
      @role.destroy
     redirect_to action:  'index' 
    end
end 
class用户:角色\u用户
拥有多个:角色\u用户
#包括默认设计模块。其他可供选择的项目包括:
#:token_authenticable,:confirformable,
#:可锁定,:可超时和:可全授权
设计:数据库可验证,可注册,
:可恢复,:可记忆,:可跟踪,:可验证
#设置模型的可访问(或受保护)属性
属性可访问:用户名、电子邮件、密码、密码确认、,
:记住我,:名字,:姓氏,:是管理员,:联系人,:出生日期,
:加入日期,:是否活动,:是否人力资源,:是否经理,:用户代码,:指定
#可访问属性:标题,:正文
结束
角色.rb

class User < ActiveRecord::Base
 has_many :roles,  :through => :role_users
 has_many :roles_users
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email, :password, :password_confirmation, 
  :remember_me, :first_name, :last_name, :is_admin, :contact_no, :birth_date,
   :joining_date, :is_active, :is_hr, :is_manager, :user_code, :designation
  # attr_accessible :title, :body
end
  class Role < ActiveRecord::Base
      attr_accessible :name
      has_many :users,  :through => :role_users
      has_many :role_users
    end

role_user.rb



 class RolesUser < ActiveRecord::Base
      attr_accessible :role_id, :user_id
      belongs_to :user 
      belongs_to :role
    end
class RolesController < ApplicationController
  before_filter :authorize_admin!

    def index
        @roles = Role.all
    end

    def new
     @role = Role.new 
    end

    def create
      @role  = Role.new(params[:role])
         if @role.save
           flash[:success] = "role created!"
           redirect_to role_path(@role)
       else
            render 'new' 
        end 
    end

    def show
      @role = Role.find(params[:id])
    end

    def edit
      @role = Role.find(params[:id])
    end

    def update
      @role = Role.find(params[:id])
    if @role.update_attributes(params[:role])
        flash.notice = "Role #{@role.name} has been updated"
        redirect_to role_path(@role)
    else 
       render 'edit'
    end
   end 

    def destroy
      @role = Role.find(params[:id])
      @role.destroy
     redirect_to action:  'index' 
    end
end 
类角色:角色\u用户
拥有多个:角色用户
结束
角色_user.rb
类RolesUser
角色\u controller.rb

class User < ActiveRecord::Base
 has_many :roles,  :through => :role_users
 has_many :roles_users
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email, :password, :password_confirmation, 
  :remember_me, :first_name, :last_name, :is_admin, :contact_no, :birth_date,
   :joining_date, :is_active, :is_hr, :is_manager, :user_code, :designation
  # attr_accessible :title, :body
end
  class Role < ActiveRecord::Base
      attr_accessible :name
      has_many :users,  :through => :role_users
      has_many :role_users
    end

role_user.rb



 class RolesUser < ActiveRecord::Base
      attr_accessible :role_id, :user_id
      belongs_to :user 
      belongs_to :role
    end
class RolesController < ApplicationController
  before_filter :authorize_admin!

    def index
        @roles = Role.all
    end

    def new
     @role = Role.new 
    end

    def create
      @role  = Role.new(params[:role])
         if @role.save
           flash[:success] = "role created!"
           redirect_to role_path(@role)
       else
            render 'new' 
        end 
    end

    def show
      @role = Role.find(params[:id])
    end

    def edit
      @role = Role.find(params[:id])
    end

    def update
      @role = Role.find(params[:id])
    if @role.update_attributes(params[:role])
        flash.notice = "Role #{@role.name} has been updated"
        redirect_to role_path(@role)
    else 
       render 'edit'
    end
   end 

    def destroy
      @role = Role.find(params[:id])
      @role.destroy
     redirect_to action:  'index' 
    end
end 
class RolesController

基本上,我希望从Users表的Roles表中的下拉列表中获取数据,并希望在创建user时保存数据。有人能帮我吗。如果您需要粘贴更多代码,请告诉我。

在您的用户对象上有一个名为“role_id=”的方法,允许您为此分配数组。如果下拉列表是multi,它将为用户提交一个参数,该参数在您的params对象中如下所示:

params => { :user => { :role_ids => [1,2,3] } }
在表单中,您可以创建如下下拉列表

<%= f.select 'role_ids[]', @roles.map{|r| [r.name, r.id]}, {}, :multiple => true %>
true%>
试试看它是否适合你

编辑

这里不需要支架

<%= f.select :role_ids, @roles.map{|r| [r.name, r.id]}, {}, :multiple => true %>
true%>

我通过使用得到了解决方案。

嗨,Swards,我尝试了你的建议,但我得到了以下错误:--namethodError in Admin/users#new Showing/home/vinay/workspace/merlin_vertis/app/views/Admin/users/_form.html.erb其中第79行出现了:未定义的方法“map”表示nil:NilClass。知道为什么会发生这种情况吗。@VSiingh尝试将
@roles.map
替换为
Role.all.map
,在这种情况下不需要使用实例变量。至于在创建操作中保存关联,我认为您需要类似于
@users.role\u users hi@Swards的内容,在usign role.all.map之后,现在我得到了“undefined method`role\u id[]”for#错误。我的建议。这是role_id[]还是role_id[]。@VSiingh-是复数。这是一种动态方法,它基于您与多个角色建立的关联。您好@Cyle,在usign Role.all.map之后,现在我得到了“未定义的方法`Role_id[]”for#错误。我的建议。这是角色id[]还是角色id[]。