选择多重选择

问题描述 投票:1回答:2

我正在学习RoR并遇到选择导轨的问题,不知道我做错了什么。非常感谢您的帮助。

尝试选择rails并选择Jquery但无法使其工作。

的Gemfile:

source 'https://rubygems.org'

ruby '2.4.1'
gem 'rails', '~>5.0.6'
# gem 'mysql2'
gem 'pg'
gem 'responders'
gem 'fast_blank'
gem 'axlsx', git: 'https://github.com/randym/axlsx.git', ref: 'c8ac844' ### to get rid of Ruby 2.4+ deprecation errors
gem 'axlsx_rails' # must come before spreadsheet_architect, see patch in config/initializers/axlsx_rails_spreadsheet_architect_patch.rb
gem 'spreadsheet_architect'
gem 'activerecord-import'

gem 'devise'
gem 'devise_invitable'
gem 'cancancan'
gem 'rolify'
gem "paranoia"
gem 'pretender'
gem 'audited'

# gem 'chosen-rails'

gem "paperclip"
gem "paperclip_utils"
gem 'search_cop'
gem 'sucker_punch'
gem 'rearmed'
gem 'rearmed_rails'
gem 'premailer-rails'

gem 'slim-rails'
gem 'simple_form'
gem 'kaminari'
# gem 'bootswatch'
# gem 'bootstrap'
gem 'jquery-rails'

gem 'cocoon'
gem 'bootstrap-sass'
gem 'font-awesome-rails'
gem 'autoprefixer-rails'
gem 'sass-rails'
gem 'uglifier'
gem 'bootstrap-modal-rails'
gem 'toastr-rails'
gem 'remotipart', github: 'mshibuya/remotipart'
# gem 'rails_admin', '>= 1.0.0.rc'

source "https://rails-assets.org" do
  gem 'rails-assets-autoNumeric','~>1'
  gem 'rails-assets-bootstrap-datepicker'
  gem 'rails-assets-jquery', '~>2'
  gem 'rails-assets-jquery-ujs'
  gem 'rails-assets-autosize', '~>3'
  gem 'rails-assets-select-sync'
  gem 'rails-assets-bootstrap3-typeahead'
  gem 'rails-assets-floatThead'
  gem 'rails-assets-chosen'
  gem 'rails-assets-chosen-bootstrap'
  gem 'rails-assets-chosen-material-theme'
  gem 'rails-assets-bootstrap-directional-buttons'
end

的application.js

//
//= 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.css.scss

*
 *= 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";

role.rb

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

user.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

查看/管理/用户/ _form.html.slim

= 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'

我试图在用户表单中选择多个角色,类似于https://harvesthq.github.io/chosen/中显示的示例

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

希望,我的解释已经足够了。

ruby-on-rails jquery-chosen
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.