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

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

问题描述

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

我想根据刚刚提交的内容选中或取消选中表单上的复选框.

这是我的控制器中的内容-

@show_values = false如果参数 [:check_value] == "1"@show_values = 真结尾

那么在我看来,我有以下-

 <% if @show_breakfast%><div class="row"><%= check_box_tag 'check_value', 1, {:checked =>已检查"}%>显示 X

<%其他%><div class="row"><%= check_box_tag 'check_value', 1%>显示 X

<%结束%>

这是我想要的方式,但我想知道是否有更聪明的方法来做到这一点.

解决方案

我不确定我是否理解这个问题,但如果你想保持相同的方法,我只会在视图中看到这个:

<%= check_box_tag 'check_value', 1, @show_breakfast %>显示 X

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天全站免登陆