Reputation: 1954
I'm developing a rails project and I need to load search results on particular div. Can someone explain me how to do this on rails 2.3.8 ?
This is my event model
create_table "events", :force => true do |t|
t.string "name"
t.date "start_at"
t.date "end_at"
t.datetime "created_at"
t.datetime "updated_at"
t.string "trainer_id"
t.string "venue_id"
t.string "description"
t.boolean "holy"
t.integer "nxt"
t.string "country_id"
end
This is my search form
<% form_tag "/events/find_trainer" do %>
<%= collection_select("event", "trainer_id", @trainers , :id, :name, {:prompt => true}) %>
<%= submit_tag "search" %>
Can some experts share your ideas about this ?
Upvotes: 0
Views: 285
Reputation: 47482
You can do it w/o form using observe_field
<%= collection_select("event", "trainer_id", @trainers , :id, :name, {:prompt => true}) %>
<%= observe_field "event_trainer_id",
:url => {:controller=> :events, :action => :find_trainer},
:on=>"change",
:with => "'value='+document.getElementById('event_trainer_id').value"
%>
<div id="some_id"></div>
Controller Code
def find_trainer
#Get your Search Results in the @search_results
#you get search value in params[:value]
render :update do |page|
page.replace_html 'some_id' , :partial => "some_partial", :object => [@search_results] #now you can use object @search_results in events/_some_partial.html.erb
end
end
Upvotes: 1