Reputation: 2381
I need some jQuery help on a search form I've created.
Right now, in ruby I have a <selection>
element populated from an array. The user can select one item and then Submit the query. I've successfully eliminated the nasty URL with with ajax (thanks to Ryan Bates), but was wondering if there was a cleaner way to handle submission?
Is there a way to automatically submit the form, without the user having to click on my button? For example, an on click event when the user selects an item within the selection element?
My current code:
$("#orders_search
input").click(function() {
$.get($("#orders_search").attr("action"),
$("#orders_search").serialize(), null,
"script");
return false; });
and if it helps to understand what I'm trying to do, here's my rails code:
View:
<%= select_tag :search_user, options_from_collection_for_select(@users , "id", "name") %>
<%= submit_tag "Search" %>
Model:
def self.search(search_user)
if search_user
where(:user_id => search_user)
else
scoped
end
end
Upvotes: 0
Views: 618
Reputation: 116
Can I suggest you look at this blog. It dds a class 'submittable' to the form element (checkbox, text area or select). Then use jquery to capture the change and handle the form submission.
http://trevorturk.com/2010/08/24/easy-ajax-forms-with-rails-3-and-jquery/
Upvotes: 2