【问题标题】:Select all or single objects from collection_select in Rails从 Rails 中的 collection_select 中选择所有或单个对象
【发布时间】:2012-10-12 14:21:05
【问题描述】:

我有一个通过 actionmailer 发送“页面”的应用程序。在我的视图代码中,我将其设置为选择一个特定的单元,然后分配获得电子邮件/页面的医务人员。我想在我的选择语句中包含一个选项来对所有单元进行分页,但不知道该怎么做。

查看/表单代码:

<%= form_for(@page) do |f| %>

  <%= f.label :message %>
  <%= f.text_field :message %>

  <%= f.label 'Medic'%>

  <%= f.collection_select(:unit_id, Unit.order("unit_name ASC"), :id, :unit_name, {}, {:class => 'select'})%>

<%= f.button :send, :class => 'btn btn-info' %>

<% end %>

控制器代码:

def create

    @page = Paging.new(params[:paging])
    unit = Unit.find(params[:paging][:unit_id])
    message = params[:paging][:message]


     if @page.save
       PagingsMailer.paging(unit.incharge, unit, message).deliver
       PagingsMailer.paging(unit.attendant, unit, message).deliver
       redirect_to pagings_path, notice: "Page was successfully sent."
      else
        redirect_to pagings_path
     end
  end

该选项可以在选择本身中,也可以作为另一个选择/复选框,只要忽略原始选择。

对此的任何帮助表示赞赏。

【问题讨论】:

    标签: ruby-on-rails-3 forms activerecord select


    【解决方案1】:

    我在这方面得到了一些帮助,并且能够通过在表单中​​创建一个数组数组并将“全部”传递给控制器​​以执行各个邮件来解决它。

    查看代码:

    <%= f.select :unit_id, options_for_select(Unit.order.map{ |unit| [unit.unit_name, unit.id] } + [["All Medics", "all"]]), {}, {:class => 'select'} %>
    

    控制器代码:

    def create
        message = params[:paging][:message]
    
        if params[:paging][:unit_id] == "all"
          Unit.all.each do |unit|
            @page = Paging.new(params[:paging])
            @page.unit_id = unit.id
    
            if @page.save
              PagingsMailer.paging(unit.incharge, unit, message).deliver
              PagingsMailer.paging(unit.attendant, unit, message).deliver
              flash.notice = "Page was successfully sent."
            end
          end
        else
          @page = Paging.new(params[:paging])
          unit = Unit.find(params[:paging][:unit_id])
    
          if @page.save
            PagingsMailer.paging(unit.incharge, unit, message).deliver
            PagingsMailer.paging(unit.attendant, unit, message).deliver
            flash.notice = "Page was successfully sent."
          end
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多