4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

チェックボタンのアンカーの範囲を広げる

Last updated at Posted at 2015-07-15

チェックボタンをつけたときにどちらにチェックが付いているのか判別したかったのとそれをカラムとして保存したかったときにちょろっと詰まったのでメモしときます。

レファレンスとかもろもろに従って最初に書いたコードが以下です。
たしかこんな感じだったような・・・
radio_buttonのレファレンス
[f.labelのレファレンス]
(http://railsdoc.com/references/label)

hoge.erb
<%= form_for @user, url: hogehoge_path do |f| %>      
  <div class="glay">
    <%= f.radio_button :hoge_flg, true %>
    <%= f.label :hoge_flg_true,"Yes" %>
  </div>
  <div class="glay">
    <%= f.radio_button :hogee_flg, false %>
    <%= f.label :hoge_flg_false,"No" %>  
  </div>
  <%= render partial: 'form_submit' %>
<% end %>

このコードだとYes Noの文字の部分かチェックボックスをクリックしないとチェックボックスにチェックが入らないという状態になり、それを囲んでいるdivのglayが意味ない感じになっています。
divのglayで囲んでいる部分をどこでクリックしてもチェックできるようにしました。
以下です。

hoge.erb
<%= form_for @user, url: hogehoge_path do |f| %>
  <%= f.label :hoge_flg_true do %>
    <div class="glay">
      <%= f.radio_button :hoge_flg, true %>
      Yes
    </div>
  <% end %>
  <%= f.label :hoge_flg_false do %>
    <div class="glay">
      <%= f.radio_button :hoge_flg, false %>
      No
    </div>
  <% end %>
  <%= render partial: 'form_submit' %>
<% end %>

正直labelでブロックを使用しその支配下にするなんて方法を知らなかったので今回なかなか感動しました。
なにかのときに参考になれば幸いです。。。。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?