2016-11-18 56 views
0

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

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

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

代码:

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

控制器

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 
+0

发布您的控制器代码,包括您的params方法。是允许参数列表中的select-tag字段? – toddmetheny

+0

编辑@toddmetheny – Luis

+0

现在看看这些参数,我意识到作为“params”返回的值只是在下拉列表中选择的值,其他值不会被考虑。如何传递下拉菜单中的所有值? – Luis

回答

0

几种方法可以解决这个 - 但选择框本身没有任何意义,在这种情况下使用。不提供实用程序。这是一种方法。你有单独的成员模型吗?这似乎是你基本上试图做一个嵌套的形式。如果您没有members型号,请创建一个。分别创建一个焦点组,并将其成员的表单放在FocusGroup的展示页面上。焦点组有许多成员,并且成员属于焦点组(具有焦点组ID)。这将允许你说@ focus_group.members并让所有成员回来。

如果由于某种原因(我认为这不是最佳),您必须将它们添加到相同的表单中,那么您将需要通过嵌套表单与表单构建器进行嵌套,从而允许您一次添加多个表单。你也可以创建一个服务对象,但这更复杂。

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

+0

我明白你的意思前两个选项我没有立即使用,因为我正在开发这个应用程序的同时学习rails,因为我没有太多时间。我知道它不会奏效,但不要担心,因为它对这个软件没有商业目的。即使在这种情况下,第三种选择是最好的选择,但我该怎么做?当我把这个数组放在一个字段中,并通过params将它分配给我的控制器时,你可以创建一个示例代码吗? 非常感谢您的关注。 – Luis

+0

哈哈 - 这是最糟糕的选择!第一个是最容易做和最好的!你会省下一些痛苦。但是如果你想要第三个选项......你可以为'members_added'创建一个'hidden_​​field',然后每次你点击add时,获取该hidden_​​field的值并更新数组。然后,当你提交时,所有这些值将在你隐藏的领域。 – toddmetheny