使用form_for时,Check_box标记返回空数组位置

时间:2012-03-16 19:33:05

标签: ruby-on-rails ruby

之前我使用带有多个复选框的form_tag的check_box_tag。这返回了一个像[“student”,“teacher”]这样的参数中的数组。我只是通过在控制器中创建一个函数并使用参数来计算由共享它们的用户显示的帖子。参数看起来像

user_type=>["student","teacher"] 

但现在我转移到form_for标签,因为我使用的是不同的模型,我希望参数格式正确。现在通过的参数看起来像

filter_obj=>{user_type=>["","",""student,"teacher"]}

这是我的复选框代码

<% User::OPEN_USER_TYPES.each do |type|%>
<div class="radio-check-group <%='hide-group' if container == "my-contribution"  %>">
            <div class="control"><%= f.check_box(:user_type,{:multiple => true},type,nil)%></div>
            <div class="label"><%= f.label(:user_type,type,:value=>type)-%></div>
</div>

<% end %>

有没有办法让表单传递像filter_obj =&gt; {user_type =&gt; [“student”,“teacher”]}这样的参数。我不想在我的控制器中进行额外的清理处理。

2 个答案:

答案 0 :(得分:1)

我认为您需要:user_type之后的method name

f.check_box(:user_type,[method_name],{:multiple => true},type,nil) #method name might be 'occupation'

这有帮助吗?

答案 1 :(得分:1)

我不知道如何使用check_box这样做。您可以使用check_box_tag代替check_box,但它不像check_box

那样优雅
<% User::OPEN_USER_TYPES.each do |type|%>
    <div class="radio-check-group <%='hide-group' if container == "my-contribution"  %>">
        <div class="control">
           <%= check_box_tag "filter_obj[user_type][]", type, @filter_obj.user_type.to_a.include?(type) %>
        </div>
        <div class="label">
           <%= f.label(:user_type,type,:value=>type)-%>
        </div>
    </div>
<% end %>
相关问题