Rails 中针对多个模型的 Ajax 表单部分
我有这段AJAX代码(取自:http://badpopcorn.com/blog/2008/09/11/rails-observer-field/),存在于许多视图中(主要是 new.html.erb 和 edit.html.erb) 。为了避免代码重复,我假装使用部分:
app/views/cities/_state_city_form.html.erb
<p> State:
<%= select(:state, :state_name, City.all(:select => 'DISTINCT(state)', :order => :state).collect {|c| [ c.state.titleize, c.state ] }, {:include_blank => true})%>
<%= observe_field 'state_state_name', :url => { :action => :by_state,
:controller => :cities },
:update => 'cities_list',
:with => "'state='+ $('#state_state_name').val()" %>
</p> City:
<div id="cities_list">
<%= select(modelname.to_sym, :city_id, [], {:include_blank => true})%>
</div>
<p>
</p>
另外,我还有 Ajax 回调内容:
app/views/cities/by_state.html .erb
<%= select(/*PROBLEM_HERE*/, :city_id, @cities.collect {|c| [ c.name, c.id ] }, {:include_blank => true})%>
和我的城市控制器:
app/controllers/cities_controller.rb
class CitiesController < ApplicationController
def by_state
@cities = City.find_all_by_state(params[:state])
render :layout => false
end
end
然后我只需在每个需要的地方调用部分:
app/views/user/new。 html.erb
...
<%= render :partial => 'cities/state_city_form', :locals => { :modelname => :user } %>
...
app/views/employee/new.html.erb
...
<%= render :partial => 'cities/state_city_form', :locals => { :modelname => :employee } %>
...
在这里我可以将 :user 或 :employee 传递给部分,但我如何将它们传递给 Ajax 回调?
I have this piece of AJAX code (took from: http://badpopcorn.com/blog/2008/09/11/rails-observer-field/) that is present in many views (new.html.erb and edit.html.erb mostly). To avoid code duplication I pretend to make use of partials:
app/views/cities/_state_city_form.html.erb
<p> State:
<%= select(:state, :state_name, City.all(:select => 'DISTINCT(state)', :order => :state).collect {|c| [ c.state.titleize, c.state ] }, {:include_blank => true})%>
<%= observe_field 'state_state_name', :url => { :action => :by_state,
:controller => :cities },
:update => 'cities_list',
:with => "'state='+ $('#state_state_name').val()" %>
</p> City:
<div id="cities_list">
<%= select(modelname.to_sym, :city_id, [], {:include_blank => true})%>
</div>
<p>
</p>
Also I have the Ajax callback content:
app/views/cities/by_state.html.erb
<%= select(/*PROBLEM_HERE*/, :city_id, @cities.collect {|c| [ c.name, c.id ] }, {:include_blank => true})%>
And my cities controller:
app/controllers/cities_controller.rb
class CitiesController < ApplicationController
def by_state
@cities = City.find_all_by_state(params[:state])
render :layout => false
end
end
Then I would simply call the partial in every from where's is needed:
app/views/user/new.html.erb
...
<%= render :partial => 'cities/state_city_form', :locals => { :modelname => :user } %>
...
app/views/employee/new.html.erb
...
<%= render :partial => 'cities/state_city_form', :locals => { :modelname => :employee } %>
...
Here I can pass :user or :employee to the partial, but how can I pass them to the Ajax callback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将模型名称作为字符串传递给部分并使用 to_sym 将其转换为符号。
希望这有帮助。
You can try to pass the model name as string to the partial and convert it to symbol using to_sym.
Hope this helps.