【问题标题】:RoR - dynamic dropdown with checkboxesRoR - 带有复选框的动态下拉列表
【发布时间】:2016-04-18 13:50:53
【问题描述】:

我希望实现一个带有复选框的动态下拉列表,以便消息的发送者可以选择多个接收者。

目前我在消息文本区域下只有一个收件人列表。

_recipients.html.erb:

<% @recipients.each do |user| %>
  <label for="user<%= user.id %>" >
    <span class="list-group-item"><%= user.full_name %></span>
    <%= check_box_tag "message[user_tokens][]", user.id, @message.users.include?(user), id: "user#(user.id)", class: "form-control" %>
  </label>

用户.rb:

class User < ActiveRecord::Base

  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :full_name, presence:true,
            length: {minimum: 4 }

  has_many :messages, foreign_key: :sender_id
  has_many :notes
  has_many :homeworks
  has_many :hw_comments
  belongs_to :classmodule
  belongs_to :student

recipient.rb:

class Recipient < ActiveRecord::Base
    belongs_to :message
    belongs_to :user

    validates :user_id, presence: true
end

messages_controller.rb:

class MessagesController < ApplicationController
    before_action :authenticate_user!

    def new
        @message = Message.new
        @recipients = User.all 
        @recipients = @recipients.page(params[:page]).per(8)
    end

    def create
        @message = current_user.messages.build(message_params)

        if @message.save
            flash[:success] = "Message sent"
            redirect_to messages_path
        else
            flash[:alert] = "Something went wrong"
            render :new
        end
    end

    def index
        @messages = Recipient.where(:user_id => current_user.id).order('created_at DESC')
    end

    private
    def message_params
        params.require(:message).permit(:title, :body, :sender_id, user_tokens: [])
    end 
end

例如,是否可以使用 collection_select 将复选框和名称放入下拉列表中?如果需要,很高兴提供更多代码。谢谢。

【问题讨论】:

  • 你到底想要什么?选择给定消息的收件人列表?
  • 是的,这就是我想做的。

标签: ruby-on-rails checkbox grouped-collection-select


【解决方案1】:

如果您使用的是引导程序,您可以使用下拉菜单执行此操作,请参阅示例 http://www.bootply.com/8V6EpWyMO3

(尽管您可能会考虑在 js 中进行一些手动覆盖,以了解点击对所点击内容的敏感程度)

导轨部分

如果您不使用简单表单并且确实需要引导解决方案,则需要编写自定义输入。

【讨论】:

  • 谢谢,链接有效,我只是不知道如何在 Rails 中做到这一点。我不想在名称中硬编码。我想直接从表格中获得动态。
  • 您使用的是 simple_form_for 还是常规的 form_for -- 您使用的是引导程序作为您的 css 框架吗?我可以为您提供详细信息,但需要知道您从什么开始。
【解决方案2】:

有不同的方法,例如将bootstrap select 与多个选项一起使用,这样您就只有一个选项可以选择多个选项。你必须调整你的message_params

另一种方法是使用 simple_formcocoon,作为 cocoon 处理关联模型上新“视图”的渲染。可能就像拥有一样简单:

(为了方便使用 slim)

recipient_fields.slim

.nested-fields
  = f.collection_select :user_id, User.all, :id, :name
  = link_to_remove_association 'remove recipient', f

并在每次您想添加新收件人时呈现一个新视图:

link_to_add_association,通过茧处理html元素的渲染和销毁。

【讨论】:

  • 谢谢。我正在寻找的最好是我们的集合选择方法,该方法允许名称和复选框位于下拉列表中。不过,可以通过其他方式实现这一目标。
猜你喜欢
  • 2010-12-02
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2015-03-22
  • 2016-05-05
  • 2010-11-21
相关资源
最近更新 更多