Rails - 获取复选框值并根据参数设置检查状态的最佳方法 [英] Rails - best way to get checkbox value and set checked status based on parameter

查看:151
本文介绍了Rails - 获取复选框值并根据参数设置检查状态的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails中有一个表单,当提交时,重新加载与参数相同的页面并显示一些数据。



我在表单上有一个复选框



以下是我在控制器中所拥有的内容:

  @show_values = false 

如果params [:check_value] ==1
@show_values = true
end



然后在我看来,我有以下 -

 <%if @ show_breakfast%> 
< div class =row><%= check_box_tag'check_value',1,{:checked => checked}%>显示X< / div>
<%else%>
< div class =row><%= check_box_tag'check_value',1%>显示X< / div>
<%end%>

这是我想要的方式,但我想知道是否有一个更聪明的方式。

解决方案

我不知道我是否明白这个问题,但如果你想保持相同的方法,在视图中具有以下内容:

 < div class =row><%= check_box_tag'check_value',1 ,@show_breakfast%>显示X< / div> 


I have a form in Rails that when submitted, reloads the same page with the parameters and displays some data.

I have a checkbox on the form that I want to have checked or unchecked based on what was just submitted.

Here's what I have in my controller-

@show_values = false

  if params[:check_value] == "1"
    @show_values = true
  end

Then in my view, I have the following-

   <% if @show_breakfast%>
      <div class="row"><%= check_box_tag 'check_value', 1,  {:checked => "checked"}%> Show X</div>
    <% else %>
      <div class="row"><%= check_box_tag 'check_value', 1%> Show X</div>
    <% end %>

This is working the way I want, but I'm wondering if there's a smarter way to do this.

解决方案

I'm not sure if I understood the question, but if you want to keep the same approach, I would only have this in the view:

<div class="row"><%= check_box_tag 'check_value', 1,  @show_breakfast %> Show X</div>

这篇关于Rails - 获取复选框值并根据参数设置检查状态的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆