Ruby-on-rails – make boolean form field hidden

hidden-fieldruby-on-railssimple-form

I have a boolean to make comments public or private.

The boolean is a column in submissions and I have it working in a clumsy way right now and would like to eliminate the checkbox from the form, replacing with a hidden field, so that all the user sees is the submit button, conditional based on the boolean state:

submissions#show:

<% if @submission.comment_show == true %>
    <%= render "hide_comment_form" %>
    <%= render "comments/comment" %>
 <% else %>
    <%= render "show_comment_form" %>
 </div>
 <% end %>

_show_comment_form

<%= simple_form_for [@contest, @submission] do |f| %>
   <div>
     <%= f.input :comment_show, label: false %>
     <%= hidden_field_tag :contest_id, @contest.id %>
         <%= f.submit "Make Comments Public", :class => 'btn btn-mini' %>
   </div>
 <% end %>

_hide_comment_form

 <%= simple_form_for [@contest, @submission] do |f| %>
       <div class ="">
      <%= f.input :comment_show, label: false %>
      <%= hidden_field_tag :contest_id, @contest.id %>
      <%= f.submit "Make Comments Private", :class => 'btn btn-mini' %>
    </div>
 <% end %>

I have tried hidden_field_tag, but haven't had any luck getting it to work.

Also, I've seen some fancier methods and routing to accomplish the same thing:
http://buckybits.blogspot.com/2011/09/simple-ajax-property-toggle-in-rails-30.html

But I would prefer to use a hidden field and conditionals to keep it simple.

Is it possible to use a hidden field to set a boolean in a form or do I have to go with a custom method and routing?

Best Answer

See the answer to this SO question: rails simple_form - hidden field - create?

Since you are using simple form, you can do something like this:

 f.input :contest_id, :as => :hidden, :input_html => { :value => @contest.id }