多项选择

时间:2019-02-19 08:55:30

标签: ruby-on-rails jquery-chosen

我正在学习RoR,并且在选择铁轨时遇到问题,不确定我在做什么错。感谢您的帮助。

尝试选择了rails并选择了Jquery,但无法使其正常工作。

gemfile:

//
//= require chosen
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require jquery.easing/js/jquery.easing.js
//= require bootstrap/dist/js/bootstrap.js
//= require grayscale.js
//= require bootstrap-sprockets
//= require toastr
//= require_tree .

window.initChosen = function(){
  $('.is-chosen').chosen({
    width: "240px",
    search_contains: true,
    placeholder_text_multiple: 'Select...'
  });

  $('.chosen-100').chosen({
    width: "100%",
    search_contains: true,
    placeholder_text_multiple: 'Select...'
  });
};

application.js

*
 *= require chosen-material-theme
 *= require chosen_ie11_patch
 *= require bootstrap-modal
 *= require bootstrap-datepicker
 *= require bootstrap-directional-buttons
 *= require font-awesome
 *= require toastr
 */
 @import "bootstrap-sprockets";
 @import "chosen";
 // @import 'bootstrap';
 @import 'sass/main.scss';
 @import "font-awesome";

application.css.scss

class Role < ApplicationRecord
has_and_belongs_to_many :users, :join_table => :users_roles


belongs_to :resource,
           :polymorphic => true,
           :optional => true


validates :resource_type,
          :inclusion => { :in => Rolify.resource_types },
          :allow_nil => true

scopify
end

role.rb

class User < ApplicationRecord
  rolify
  belongs_to :resource, polymorphic: true, optional: true

  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :database_authenticatable, :confirmable, :invitable

  attr_accessor :invited, :skip_invitation

  def self.strong_params(params)
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :deleted_at, :invited, :skip_invitation, role_ids: [])
  end

  validates :email, presence: true, uniqueness: {case_sensitive: false}

  validates_confirmation_of :password #normally only required on create

  before_validation :check_registration
  after_create :set_default_role
end

user.rb

= simple_form_for [:admin, @user] do |f|
  = f.error_notification

  / .row
  /   .col-sm-12
  /     = f.association :person, collection: (@user.new_record? ? Person.available : [@user.person]), autofocus: true, prompt: "Select a person...", input_html: {class: 'is-chosen', disabled: (true unless @user.new_record?)}
  .row
    .col-sm-5
      = f.input :first_name
      / br
      = f.input :last_name
      / br
      = f.input :email
      / br
      = f.association :roles, input_html: {class: 'is-chosen'}
      / = f.association :users,
                  collection: Role.all,
                  include_blank: true,
                  input_html: { class: 'is-chosen' }

      - if @user.new_record?
        br
        = f.input :skip_invitation, as: :boolean, input_html: {value: 'true'}

    .col-sm-5

      - unless @user.new_record?
        .well.well-small style="width:300px"
          = f.input :password, autocomplete: "off", hint: ("Leave it blank if you don't want to change it" unless @user.new_record?)
          br
          = f.input :password_confirmation

  .form-actions.space-below2.space-above2
    button.btn.btn-success type="submit" Save
    = link_to "Cancel", admin_users_path, class: 'btn btn-default space-left2'

view / admin / users / _form.html.slim

{{1}}

我正在尝试以用户形式选择多个角色,类似于https://harvesthq.github.io/chosen/

中显示的示例

相反,我没有选择多个框就得到了所有角色的列表。

希望,我的解释就足够了。

2 个答案:

答案 0 :(得分:1)

您缺少选项multiple: true,请在此处设置多个选项,例如:input_html: { multiple: true }

答案 1 :(得分:1)

已解决。问题是由于turbolinks,js没有重新加载。我必须使用以下内容: $(document).on('turbolinks:load',function(){     选择...代码 });