Hiding the checkbox and assigning a value - Ruby on Rails - simple question

I am trying to hide the checkbox and assign a default value of 1, which is only displayed by the submit button. Here is my form. Just wondering how the right format is, since I'm new to rails. I think you can do this with helpers, but I was wondering if I could 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" %> 
+7
ruby ruby-on-rails forms
source share
3 answers

You can do this, but if all you need to do is set the parameter without displaying the field, then most likely you will want this hidden field:

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

If you really need a hidden flag (maybe you can additionally display it later using javascript?), You can do it like this:

 <%= f.check_box :vote, :checked => true, :style => 'visibility: hidden' %> 
+10
source share

You can use CSS to hide this check box:

 <%= 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 %> 
+1
source share

If you just want to pass the value together, use a hidden field

 <% remote_form_for [@post, Vote.new] do |f| %> <%= f.hidden_field_tag 'vote', '1' %> <%= f.submit "Vote" %> <% end %> 
0
source share

All Articles