Get value of a hidden field tag Ruby on Rails - ruby

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.

Related

How to save different values for the checkbox field labels, in ruby on rails?

I have a collection checkboxes in my form, like below.
<% ["cricket" ,"tennis", "not there in list"].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
Currently the label of the checkbox, and its value that is getting saved in the db, both are same. i want to save different values to the db for the labels. i tries like below. but its not working. can anyone help me with this.
<% [["cricket","cri"] ,["tennis","ten"], ["not there in list","na"]].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓
<% [["cricket","cri"], ["tennis","ten"], ...].each do |name, val| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(val)}, name, "" %> <%= name %></p>
</div>
<% end %>

Rails submit remote form onclick of radio button

I have a form that submits when you chose a radio button (below). Currently the page refreshes when it submits and I'd like to use Jquery and remote submission so it doesn't refresh. Any idea where to start with this?
<%= simple_form_for(#order) do |f| %>
<%= f.input :orderstatus_id, :as => :radio, :label => false do %>
<% current_user.account.orderstatuses.order(:status_order).each do |os| %>
<p id="invoice-color-select">
<%= f.radio_button :orderstatus_id, os.id, :class => 'selector', :onclick => "this.form.submit();" %>
<%= f.label "#{os.name}", :style => "background: ##{os.color}", :class => 'status' %>
</p>
<% end %>
<% end %>
<div style="margin-top:20px">
<% if !current_user.account.has_plan? || current_user.account.plan_key== "ultra" %>
<a class="fancybox" href="#editstatus">Edit</a>
<% end %>
</div>
<% end %>
I added this into my view:
<%= f.radio_button :orderstatus_id, os.id, :class => 'selector', :onclick => "this.form.submit();" %>
You can see the :onclick portion here.

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.

semantic_form_for with an alert upon completion

I am using semantic_form_for and I wonder if there is such attribute as :complete then upon successful creation, the form would alert something so that the user would have to click on "Okay" to proceed?
Do you mean confirmation before submit?
you could use something like the following:
<%= semantic_form_for #user do |f| %>
...
<%= f.buttons do %>
<%= f.commit_button :button_html => {:confirm => 'Are you sure?'} %>
<% end %>
<% end %>
My solution is like the following and it works well:
<%= semantic_form_for #post do |f| %>
...
<%= f.action :submit, :button_html => { 'data-confirm'.to_sym => 'Are you sure?' } %>
<% end %>

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

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)

Resources