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

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" %>

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' %>

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 %>

If you just want to pass the value along, use a hidden field
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field_tag 'vote', '1' %>
<%= f.submit "Vote" %>
<% end %>

Related

Form submit button not showing

I'm working on a project where I'm trying to implement a voting system, but I'm having some trouble, I've tried creating a form with a hidden field:
<% form_for :vote, url: votes_path do |f| %>
<%= f.hidden_field :rating, value: '1' %></td>
<%= form_submit_button("vote 1") %>
<% end %>
But when I run that the submit button does not appear, I'm not really sure what I'm doing wrong.
form_for generates <FORM> tag, so you need to include its output with <%=:
<%= form_for :vote, url: votes_path do |f| %>
Use this code:
<%= form_for :vote, url: votes_path do |f| %>
<%= f.hidden_field :rating, value: '1' %></td>
<%= form_submit_button("vote 1") %>
<% end %>
= is usually shows the form in view page, if you remove it then any input tag will not be displayed.

Build table dynamically depending on selection in rails

I am looking for the possibility to build a table in my view dynamically, depending on selection.
Currently i have 2 combox ,2 submit buttons and a text_area in my view eg.:
<%= select_tag :user_selected, options_from_collection_for_select(#user, 'id', 'lastname') %>
<%= select_tag(:rights_id, options_for_select([['Read', 1], ['Read/Write', 2], ['Read/Write/Delete', 3]])) %>
<%= submit_tag "add" %>
<%= fields_for :content do |tf| %>
<%= tf.text_area :text , :id => 'text', :cols => '100', :rows => '25' %>
<% end %>
<%= tf.submit 'Save' %>
Every time if i hit the add button, i will expand the table with my selection, but the content from the text_area shoud be unchanged.
Is this possible in rails? If yes, how can i do it?
You can use a remote form + partials, and js.rjs file will refresh the table in main window.
It seems it looks like this getting started example mixed with this ajax mini-tutorial example, both from Rails Guides.
note this, from Working with javascript:
<b>Users</b>
<ul id="users">
<% #users.each do |user| %>
<%= render user %>
<% end %>
</ul>
<br>
<%= form_for(#user, remote: true) do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
You have to use Javascript or Javascripts frameworks like jquery. You have to write the event on add button in javascript(or jquery)

make boolean form field hidden

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?
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 }

form select is not remembering value

Select helper is not remembering its database value. But Text field is remembering. Any ideas? Thanks.
<%= form_for([:admin, #product], :remote => true ) do |f| %>
<%= f.text_field :name %>
<%= f.select(:search_status, ["0","1","2"]) %>
<% end %>
It's likely that search_status is an integer and not matching the strings in your array. Does it work if you use [0,1,2] instead?

Rails checkboxes and serialized data

I'm trying to figure out whats the best way to get checkboxes to properly show their current state. This is what I have in my form
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= f.fields_for :notification_preferences, #user.notification_preferences do |p| %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_request %>
<%= p.check_box :notify_on_friend_comment %>
<% end %>
<%= f.submit %>
<% end %>
notification_preferences is a serialized hash on my user model
class User < ActiveRecord::Base
serialize :notification_preferences, Hash
My issue that is no matter what I try, I can not get the check boxes to reflect the existing state of the hash values. IE, if the hash already contains :notify_on_friend_post => 1, then the check box for that value should be checked.
The form posts the data fine, and I'm able to update my model as well.
Update
using check_box_tag I can get this to work
<%= p.hidden_field :notify_on_friend_post, :value => "0" %>
<%= check_box_tag "user[notification_preferences][notify_on_friend_post]", "1", #user.notification_preferences[:notify_on_friend_post] == "1" ? true : false %>
ugly but working, still hoping I'm missing something very obvious
I ran into this problem and solved it in a simpler way.
<%= form_for #user do |f| %>
<%= f.fields_for :notifications, #user.notifications do |n| %>
<%= n.check_box :new_task, checked: #user.notifications[:new_task] == "1" %>
<% end %>
<%= f.submit %>
<% end %>
In this way you let the magic of check_box to the work and don't need to have a hidden_field because Rails will provide one for you. Still unsure why you need a "checked" field, but it seemed to not work without one.
Try something like this:
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= check_box_tag "user[notification_preferences][]", :notify_on_friend_post, #user.notification_preferences.try(notify_on_friend_post) %>
<%= f.submit %>
<% end %>

Resources