how to insert null value into DB using radio_button and form_for formhelper - ruby

I don't want to argue about wether or not a null value can be considered a boolean - I know it can't. I still, however, want to insert a null value into the (postgres) DB. The column is of type boolean. How can I accomplish this with a radio button? Here is what I've tried:
<div class="new-partner-form">
<%= form_for [:admin, matching_profile.partner, matching_profile], :html => {:id => "edit_profile", :multipart => true} do |f| %>
<div class="rounded-block semi-wide clear">
<h4>Military Service</h4>
<%= f.radio_button :served_in_us_army, false %>
<%= label :served_in_us_army, 'NO', {:style => 'display:inline'} %>
<%= f.radio_button :served_in_us_army, true %>
<%= label :served_in_us_army, 'YES', {:style => 'display:inline'} %>
<%= f.radio_button :served_in_us_army, nil %>
<%= label :served_in_us_army, 'NO PREFERENCE', {:style => 'display:inline'} %>
<%= f.error_message_on :served_in_us_army %>
</div>

What about just setting a default value for the column of null? In your form, instead of using the f.radio_button, just use plain old html and set the name so that it isn't picked up with the structure of what you are submitting for your create action.

It looks like you need not boolean but enumerable value, because boolean should be true or false only)

Related

Don't show empty field in rails4

<p>
<% if #person.name %>
<strong>Name:</strong>
<%= #person.name %>
<% end %>
</p>
<p>
<% if #person.gender %>
<strong>Gender:</strong>
<%= #person.gender %>
<% end %>
</p>
<p>
<% unless #person.age.blank? %>
<strong>Age:</strong>
<%= #person.age %>
<% end %>
</p>
<p>
<% unless #person.address.blank? %>
<strong>Address:</strong>
<%= #person.address %>
<% end %>
</p>
This code works fine. Its not showing empty field , but I want to know that is there any other way to do so. Because here I'm repeating same type of code again and again. Can I use any helper that stop showing empty fields?
There are many ways to do this, and the 'best' is dependent on your situation. As long as the labels are always the same as the attributes, one way you could do it is in a simple partial:
#person/_attribute.html.erb
<% if #person.public_send attribute != nil %>
<strong><%= attribute.to_s.capitalize %></strong>
<%= #person.public_send attribute %>
<% end %>
which would make your view look like this:
<p>
<%= render 'attribute' :attribute => :name %>
</p>
<p>
<%= render 'attribute' :attribute => :gender %>
</p>
<p>
<%= render 'attribute' :attribute => :age %>
</p>
<p>
<%= render 'attribute' :attribute => :address %>
</p>
I have to take this opportunity to preach HAML at you, though - your code could look like this!
#person/_attribute.haml
- if #person.public_send attribute != nil
%strong= attribute.to_s.capitalize
= #person.public_send attribute
#person/show.haml
%p= render 'attribute' :attribute => :name
%p= render 'attribute' :attribute => :gender
%p= render 'attribute' :attribute => :age
%p= render 'attribute' :attribute => :address

Formtastic Creating a form without users

I'm creating a web application with formtastic. I installed the gem and wrote my code like this:
<%= semantic_form_for #index do |form| %>
<%= form.inputs do %>
<%= form.input :name %>
<%= form.input :born_on, :start_year => 1997 %>
<%= form.input :description, :as => :text %>
<%= form.input :female, :as => :radio, :label => "Gender", :collection => [["Male", false], ["Female", true]] %>
<% end %>
<%= form.actions do %>
<%= form.action :submit, :as => :button %>
<% end %>
<% end %>
I want the form to appear on the index page which is why I have #index. For some reason I can't do #index. How would I reference the top line so that it renders a form on the index page? Currently my index page has nothing in it, but it is defined in the controller
form_for helper expects some object that responds to fields You refer inside Your form. Here is an example of using semantic_form_for with plain Ruby object: http://affy.blogspot.com/2010/02/using-formtasic-without-activerecord.html.
Also the object You specify for form doesn't effect which page is being rendered. Are You sure You are not mixing up something? Maybe if You share a bit more of Your controller code we might help You better.

Get value of a hidden field tag Ruby on Rails

I am lost, I do not know what I'm doing wrong! I have 4 radio buttons and a hidden field (value = "1"). When you click on the second radiobutton, the value of the hidden field changes to 2 and so on. This works fine with a js function.
Different divs will be showed when a different radiobutton is selected. Now, when I'm trying to get the value of the hidden field in my controller it always returns nil.
Here's the code:
view:
(radiobuttons, hiddenfield and one div)
<div>
<%= form_tag patients_path do %>
<%= radio_button_tag 'searchRBN', 'patient', true, :onchange => "checkRadioButton()" %>
<%= label_tag :byPatient_patient, "Patient" %>
<%= radio_button_tag 'searchRBN', 'staff', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byStaff_staff, "Staff" %>
<%= radio_button_tag 'searchRBN', 'ocmw', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byOcmw_ocmw, "OCMW" %>
<%= radio_button_tag 'searchRBN', 'mutuality', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byMutuality_mutuality, "Mutuality" %>
<%= hidden_field_tag :hidden_one, "1" %>
<% end %>
</div>
<div id="searchByPatient">
<%= form_tag patients_path, :method => 'get' do %>
<p>
<%= text_field_tag :search1, params[:search1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
controller:
def index
#staff_all = Staff.all
#ocmw_all = Ocmw.all
#mutuality_all = Mutuality.all
debugger
if params[:hidden_one] == '1'
#patients = Patient.searchByName(params[:search1])
elsif params[:hidden_one] == '2'
#patients = Patient.searchByStaff(params[:search2])
else
#patients = Patient.all
end
end
It's because you have two forms. When you submit the second form it won't send the fields of the first form. If you put everything in one form it will work as expected.
Use only single form:
Also as a workaround use two submit tag in a single form:
differentiate both the action with params[:action]
For Example:
<%= form_for :attachment_metadata, :url=>{:action=>'delete_files'}, :html=>{:onsubmit=> "return confirm('Are you sure, you want to delete selected files?');",:multipart => true} do |f| %>
<table>
..........Some stuff here..........
</table>
<%= submit_tag 'Reprocess', :class =>'button' %>
<%= submit_tag 'Remove', :class =>'button' %>
<% end %>
params[:commit] can differentiate the actions of two submit tags.
#action = params[:commit]
it gives #action value as "Reprocess" if your click the Reprocess button and gives "Remove" value if you click the Remove button,
Then you will get your values.

How to pass name of form_for's method to partial in rails?

Good day! Little newbee question:
let's say I want to generate fields of form_for helper of special appearance. Thus to not to repeat complex code to generate many fields of model, I render some partial and pass there parameters. There is a reference to form object amongst them. Question is what pass to render method to call in the partial necessary method of form object? For example I want to generate this time email_field:
<%= form_for #order do |f| %>
<%= render :partial => 'form_field_special', :locals => {:form => f, :type => :email_field, :labelcaption => "SpcName"} %>
<% end %>
and partial itself:
<div class="control-group">
<%= f.label :name, <%= labelcaption %> , :class => "control-label" %>
<div class="controls">
<%= f.type :name %> # ??????
</div>
</div>
thank you guys!
Use ruby power. Object#send method:
<%= form_for #order do |f| %>
<%= render :partial => 'form_field_special', :locals => {..., :field_type => 'email_field'} %>
<% end %>
<%= f.send(field_type, :name) %>
The following method proved as the fastest way to invoke ruby object's function by its name:
m = form.method(name_of_the_method)
m.call(parameters)

passing value from select_tag

I want to pass selected :ammount value to my controller as :quantity. What i am doing wrong?
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= button_to 'Add to cart', line_items_path(:product_id => product.id, :quantity => :ammount) %>
You should use form here to pass the data to controller`s action:
<%= form_tag line_items_path(:product_id => product.id) do %>
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= submit_tag 'Add to cart' %>
<% end %>
All that you did in your sources is just passing symbol :ammount to a controller`s action as a quantity param.

Resources