传递params中select_tag的值

时间:2016-11-18 13:53:39

标签: html ruby-on-rails validation ruby-on-rails-4

我创建了一个“select_tag”字段,当用户单击保存此字段的所有值以通过参数传递给控制器​​时,我需要它。问题是,当我单击“保存”时,只有在此字段中输入的第一个值才会传递给控制器​​。

为了更好地理解,“select_tag”字段通过另一个字段“f.text_field:members”填充。当我点击“添加”按钮时,“f.text_field:members”字段的值被传递到“select_tag”字段,因此我希望能够从“select_tag”字段中检查所有这些值以进行验证,保存,我该怎么办?正如我已经说过的,只传递了第一个输入值。

现在看一下params,我意识到返回“params”的值只是在下拉列表中选择的值,其他值不予考虑。如何传递下拉列表中的所有值?

代码:

<%= form_for(@focus_group) do |f| %>
  <% if @focus_group.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@focus_group.errors.count, "error") %> prohibited this focus_group from being saved:</h2>

      <ul>
      <% @focus_group.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <script type="text/javascript">
  $(function() {
    $('#focus_group_moderator, #focus_group_members').autocomplete({
      source: '/focus_groups/autocomplete.json'
    });
  });
  function add(){
    var value = $('#focus_group_members').val();
    var select = document.getElementById("membersAdded");
    var option = document.createElement("option");
    option.text = value;
    option.value = value;
    select.add(option);

    $('#focus_group_members').val("");
  }

  function removeFromSelect(){
    var select = document.getElementById("membersAdded");
    select.remove(select.selectedIndex);
  }
  </script>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :topic %><br>
    <%= f.text_field :topic %>
  </div>
  <div class="field">
    <%= f.label :moderator %><br>
    <%= f.text_field :moderator %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :members %><br>
    <%= f.text_field :members %>
    <input onclick="add()" type="button" value="Add" /><br>
    <%= select_tag(:membersAdded, options_for_select([])) %>
    <input onclick="removeFromSelect()" type="button" value="Remove" /><br>
    <br>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

CONTROLLER

class FocusGroupsController < ApplicationController
  before_action :set_focus_group, only: [:show, :edit, :update, :destroy]
  include FocusGroupsHelper

  def index
    if(params[:term])
      @profile = Profile.all.select("name", "id")

      @lista = Array.new

      @profile.each do |x|
        @lista.push(x.name)
      end

      respond_to do |format|
        format.html
        format.json { render json: @lista.to_json}
      end
    else
      @focus_groups = FocusGroup.all
    end
  end

  def show
  end

  def new
    @focus_group = FocusGroup.new
    @membersAdded

    respond_to do |format|
      format.html
      format.json { render json: Profile.all.to_json}
    end
  end

  def edit
  end

  def create
    @moderator= find_profiles_id(focus_group_params[:moderator])
    @moderator.each do |f|
      @moderator_id = f.id
    end

    @params = focus_group_params
    @params[:moderator] = @moderator_id

    @focus_group = FocusGroup.new(@params)

    if @focus_group.save
      redirect_to @focus_group, notice: 'Focus group was successfully created.'
    else
      render :new
    end
  end

  def update
    if @focus_group.update(focus_group_params)
      redirect_to @focus_group, notice: 'Focus group was successfully updated.'
    else
      render :edit
    end
  end

  def destroy
    @focus_group.destroy
    redirect_to focus_groups_url, notice: 'Focus group was successfully destroyed.'
  end

  def autocomplete
    if(params[:term])
      @profile = Profile.all.where("user_id <> 0 and is_template = 'f' and name LIKE ?", "#{params[:term]}%").select("name", "id")

      @lista = Array.new

      @profile.each do |x|
        @lista.push(x.name)
      end

      respond_to do |format|
        format.html
        format.json { render json: @lista.to_json}
      end
    end
  end

  def find_profiles_id(name)
    return Profile.all.where("name LIKE ?", "#{name}%").select("id")
  end

  def find_profiles_name(id)
    @profile = Profile.all.where("id = ?", "#{id}").select("name")

    @profile.each do |e|
      @name = e.name
    end
    return @name
  end

  private

    def set_focus_group
      @focus_group = FocusGroup.find(params[:id])
    end

    def focus_group_params
      params.require(:focus_group).permit(:name, :topic, :moderator, :description, :members, :membersAdded)
    end
end

1 个答案:

答案 0 :(得分:0)

有几种方法可以解决这个问题 - 但是在这种情况下,选择框本身并没有任何意义。没有实用程序。这是一种方式。你有独立的会员模特吗?看起来你实际上是在尝试做一个嵌套的表单。如果您没有members型号,请创建一个。单独创建一个焦点组,并将成员的表单放在FocusGroup的显示页面上。焦点组有许多成员,成员属于焦点组(具有焦点组ID)。这将允许您说@ focus_group.members并让所有成员回来。

如果您出于某种原因必须在同一表单上添加它们(我认为这不是最佳选择),您希望通过带有表单构建器的嵌套表单来添加它们,以便您添加许多人一次。您也可以创建服务对象,但这更复杂。

我更喜欢的另一个选项是将所有成员推送到一个数组,然后将其作为提交时的参数传递。不是将成员添加到选择框,而是将它们推送到数组。然后,您可以将最终数组值设置为members_added(或您为参数命名的任何内容)。您需要在控制器中解压缩该阵列以创建每个成员。我更喜欢这个。第一种选择是最干净的。我认为这是三者中第三好的方法。