Rails 3 & Ajax date_select onchange => submit - ajax

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

Related

Ruby submit_tag button not doing anything

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>

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.

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

JQuery Datepicker with nested Resources Rails 3.1

I try to use the datepicker function in my app wich runs with Rails 3.1. I got it working if I use the following in my application.js file:
$(function(){
$("#exam_deadline").datepicker();
});
Now I have a nested Resource and tried a lot of things to get this working for it as well, but didn´t have any success. With some research I found that a possible solution would be this:
$(function(){
$('.datePicker').datePicker();
});
But if I add the class to my fields, it doesn´t even work for the exam_deadline anymore. I´m not so familiar with Javascript and I hope, someone can tell me, what I´m doing wrong.
This are the snippets from my views and .js:
js:
function remove_fields (link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).parent().before(content.replace(regexp, new_id));
}
view :
exam:
<%= semantic_form_for #exam do |f| %>
<%= f.inputs do%>
<%= f.input :deadline, :as => :string,
:start_year => Time.now.year, :label => "Anmeldefrist",
:order =>[:day,:month,:year], :class => 'datePicker'%>
<% end %>
<%= f.semantic_fields_for :examdates do |builder|%>
<%= render "examdate_fields", :f => builder %>
<% end %>
<p>
<%= link_to_add_fields "Termin hinzufügen", f, :examdates %>
</p>
<%end%>
examdate_fields:
<%= f.inputs :class => 'fields' do%>
<%= f.input :date, :label =>"Termin",
:as => :string,
:order =>[:day,:month,:year], :start_year => Time.now.year,
:class=>"datePicker" %>
<%= link_to_remove_fields "Entfernen", f %>
<%end%>
my application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
<%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css",
"application", "formtastic.css", "formtastic_changes.css" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %>
<%= csrf_meta_tag %>
<%= yield(:head) %>
</head>
...
Thanks in advance for every little tip!
Just a quick guess. You're using single quotes on your try with a class name.
If that doesn't solve the issue I would try to stick with Ids instead of classnames (I don't know whether datepicker can still reference them properly with class selectors). You would just need another Id for every input and dynamically created javascript (a datepicker for every dynamically created input), instead of a static js file.
Ok, after a lot of trying I got it. I changed the js to:
$(function(){
$(".datePicker").datepicker();
$('#ui-datepicker-div').hide();
$.datepicker._defaults.dateFormat = 'dd M yy'
});
and my view:
<%= f.input :date,
:label =>"Termin",
:as => :string,
:order =>[:day,:month,:year],
:start_year => Time.now.year,
:input_html => {:class =>"datePicker" }%>
Now it works:) I hope it helps others who struggle with the same problem.

Resources