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..." } %>
Related
I want to create a form_tag in my index view to accept an id which gets passed to my populateOne method. The view has been updated properly, both the number_field_tag in the form and the submit_tag are there, but when I press the button on my server, nothing happens!
Here is my view:
<%= form_tag('/affinities/populateOne/:id', method: :put) %>
<%= number_field_tag(1) %>
<%= submit_tag 'Populate One' %><br>
Here are my routes: EDIT:
put 'affinities/populateOne/:id' => 'affinities#populateOne', :as => 'populateOne_affinity'
My populateOne method is long, so here is the relevant part:
def populateOne
userA = User.find(params[:id])
...
end
What could be the problem? All help is very much appreciated! Thanks!!!
Your form_tag is posting data to the url using put method. And your route has get method. Fix it with put:
put 'affinities/populateOne/:id' => 'affinities#populateOne', :as => 'populateOne_affinity'
View:
<%= form_tag('/affinities/populateOne/:id', method: :put) %>
Route:
get 'affinities/populateOne/:id' => 'affinities#populateOne', :as => 'populateOne_affinity'
Your form submit method and your route don't match. Your form is putting, and doing what you want it to, but the route is trying to get something that isn't there. Change the route's get to put and it should work as intended.
In my index view, I forgot to put do ... end for the form_tag:
<%= form_tag('/affinities/populateOne/:id', method: :put) do %>
<%= number_field_tag(1) %>
<%= submit_tag 'Populate One' %>
<% end %><br>
I tried the ckeditor and it works fine when adding or making a new post but the problem is when I try and edit the post.
it doesn't load properly and it doesnt show anything like the code , or even a spec, it just shows the pre default ckeditor list and it doesn't even show any data coming from the database.
any help would really be great, and info would be awesome.
I have made it into a form and then rendering it so that I can just call the function like so.
This is the _form
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text, :value => 'Default value', :id => 'sometext' %>
</p>
Now this is in my edit
<%= render 'form' %>
I tried adding the raw into it like so
<%= raw render 'form' %>
or even into the _form but it doesn't seem to show properly
after tinkering and trying the usual thing the codes show it.
<p>
<%= f.label :text %><br>
<%= f.text_field :text %>
</p>
when I try it and add this now it shows here , but the ckeditor doesn't show properly.
Just give this:
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text %>
</p>
I hope you are calling this partial inside a form_for.
Update the answer,
Just remove
:value => 'Default value',
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.
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.
Im stuck uploading multiple photos to my model when adding an extra field to the photo model. In the code i hard code "1" value to the hidden_field, but i will change it eventually. The paperclip gem raises a rollback and won't insert the photos in the post. If i erase the "hidden_field" line it will success. Any ideas on how to add extra field to the upload in the view?
<%= form_for #campaign_point_of_sale, :html => {:multipart => true }, :url => "/pos/#{#point_of_sale.id}/post/#{#campaign.id}", :method => :post do |f| %>
<%= f.hidden_field :id %>
<label>Add photo <br />
<%= f.fields_for :campaign_result_point_of_sale_photos do |builder| %>
<% if builder.object.new_record? %>
<%= builder.hidden_field :is_mount_photo, :value => "1" %>
<%= builder.file_field :photo %>
<% end %>
<% end %>
<%= f.submit(:value => "Save") %>
Got it!
Just needed to delete the whole builder with both hidden_field and file_field, so the server would not try to insert a picture without an image file (just with a hidden_field value)
This can be achivied by surrounding both inputs with a div and deleting it (with jquery) at form submit if the file_field value was empty. Pretty cheap but works!