Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding Checkbox & Assigning Value - Ruby on Rails - Easy Question

I am trying to hide a checkbox and assign a default value of 1 such that the submit button only shows. Here is my form. Just wondering as the proper format as I am new to rails. I think you can do this with helpers but was wondering if I can just include it in the form. Here is the form:

<% remote_form_for [@post, Vote.new] do |f| %>
    <p>
        <%= f.label :vote %>
        <%= f.check_box :vote %>
    </p>
    <%= f.submit "Vote" %>
like image 880
bgadoci Avatar asked Nov 19 '09 20:11

bgadoci


2 Answers

You can certainly do this, but if all you want is to set a parameter without displaying a field, what you probably want instead is a hidden field:

<%= f.hidden_field :vote, :value => '1' %>

If you really do want a hidden checkbox (maybe so you can optionally display it later using javascript?), you can do it like this:

<%= f.check_box :vote, :checked => true, :style => 'visibility: hidden' %>
like image 117
John Hyland Avatar answered Sep 23 '22 12:09

John Hyland


You could use CSS to hide the checkbox:

<%= f.check_box_tag :vote, 1, true, :style => "display: none;" %>

But if you just want to pass a value you can just use a hidden field:

<%= f.hidden_field_tag, :vote, 1 %>
like image 32
Jordan Running Avatar answered Sep 23 '22 12:09

Jordan Running