Rails 中针对多个模型的 Ajax 表单部分

发布于 2024-09-11 17:26:46 字数 2077 浏览 2 评论 0原文

我有这段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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2024-09-18 17:26:46

您可以尝试将模型名称作为字符串传递给部分并使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文