我正在开发一个rails项目,我需要在特定div上加载搜索结果。有人可以解释我如何在rails 2.3.8上执行此操作吗?
这是我的活动模型
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
这是我的搜索表单
<% form_tag "/events/find_trainer" do %>
<%= collection_select("event", "trainer_id", @trainers , :id, :name, {:prompt => true}) %>
<%= submit_tag "search" %>
有些专家可以分享您的想法吗?
答案 0 :(得分:1)
您可以使用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>
控制器代码
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