Rails 4 simple_form field depending on submit button click - form-submit

I have a simple_form with 2 submit button and depending on the button click, I would like to have one field :is_active to be true or false:
<%= f.button :submit, "active true", class: "regular", is_active: true %>
<%= f.button :submit, "active false", class: "regular", is_active: false %>
this is not working

You could try to use two submit_tag in same form check this . Using params hash to check which button was clicked.
Please try like this:
<%= submit_tag 'active true', :name => 'active_true' %>
<%= submit_tag 'active false', :name => 'active_false' %>
In controller
if params[:active_true]
# do your stuff
elsif params[:active_false]
# do your stuff
end

Related

pass a string to a hidden field in rails simple form

I am trying to build a simple message relay system from scratch. My form looks like this:
<%= simple_form_for #message do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Your message" %>
<%= f.input :target_user_id, label: "Who are you sending your message to?" %>
<%= hidden_field_tag 'user_id', current_user.id %>
<%= hidden_field_tag 'sender_email', current_user.email %>
<%= f.button :submit %>
<% end %>
I am trying to pass "current_user.email" to the message model, where "Message.sender_email" is a string, but I am getting "nil" when I look at the created Message in the console. All the other fields do get passed though.
Just add this line to your controller:
#message.sender_email = #user.email
in the message create method.

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.

prevent double click with disable_form, ruby on rails

I'm trying to prevent a form from being "double posted", when the user either clicks twice or hits submit twice.
I've seen a couple posts on this, but they haven't hit this issue per se. I can't seem to get the below to stop double-posts, and I'm getting the feeling it's related to the remote => true (using ajax to show the content on the page).
Below is my form:
<%= form_for([#posts, #comment], :remote => true) do |f| %>
<%= f.text_field :comment %>
<%= f.submit "Submit", class: "btn btn-large btn-primary", :style => 'display: none;', :disable_with => '' %>
<% end %>
Any recommendations would be great. Thank you!
Use the disable_with option
<%= submit_tag :submit, :id => 'submit_button', :value => "Create!", disable_with: "Creating..." %>
The other answer didn't work for me — I believe it was from the Rails 2 era. According to the docs, the disable_with attribute should be added within a data attribute, like so:
<%= submit_tag "Complete sale", data: { disable_with: "Please wait..." } %>

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)

Rails 3 & Ajax date_select onchange => submit

I'd like my date to get saved anytime I change my date via ajax, is this possible? I tried
<%= form_for #dates,:remote => true do |f| %>
<%= f.date_select :booking_date,:onchange => "this.form.submit();" %>
<% end %>
but it does nothing, any good work arounds?
From the rails documentation:
date_select(object_name, method, options = {}, html_options = {})
Because onchange is an html option you need to provide an empty set of options to date_select, otherwise it assumes onchange is a date_select option.
You should change your call to date_select to look like this:
f.date_select :booking_date, {}, :onchange => "this.form.submit();"
Are you including this in your layout:
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Resources