Rails 4:如何将form builder对象传递给使用javascript(jQuery)更新的嵌套表单
我正在将一个工作应用程序从Rails2.3更新(并重新分解)到Rails4.0。 该应用程序包括一个名为“智能组”的功能。智能组建立一个搜索查询,可以保存并重新用于查找一组“人员”记录 一个智能组有多个智能组规则,每个智能组规则都属于一个属性,该属性具有零个或多个运算符 创建智能组的表单还处理添加智能组规则。选择规则的属性时,应更新表单以显示所选属性的关联运算符和用于输入规则值的字段 问题在于,当选择属性时,表单需要动态更新,以便显示所选属性的运算符。这是我在Rails 4.0中无法理解的部分Rails 4:如何将form builder对象传递给使用javascript(jQuery)更新的嵌套表单,jquery,ruby-on-rails,ujs,ruby-on-rails-4,Jquery,Ruby On Rails,Ujs,Ruby On Rails 4,我正在将一个工作应用程序从Rails2.3更新(并重新分解)到Rails4.0。 该应用程序包括一个名为“智能组”的功能。智能组建立一个搜索查询,可以保存并重新用于查找一组“人员”记录 一个智能组有多个智能组规则,每个智能组规则都属于一个属性,该属性具有零个或多个运算符 创建智能组的表单还处理添加智能组规则。选择规则的属性时,应更新表单以显示所选属性的关联运算符和用于输入规则值的字段 问题在于,当选择属性时,表单需要动态更新,以便显示所选属性的运算符。这是我在Rails 4.0中无法理解的部分
class SmartGroup < ActiveRecord::Base
has_many :rules, :class_name => "SmartGroupRule", :foreign_key => "smart_group_id"
accepts_nested_attributes_for :rules, reject_if: :all_blank, allow_destroy: true
end
class SmartGroupRule < ActiveRecord::Base
belongs_to :smart_group
belongs_to :smart_group_property
belongs_to :operator
end
class SmartGroupProperty < ActiveRecord::Base
has_many :smart_group_rules
has_many :property_operators
has_many :operators, through: :property_operators
end
class Operator < ActiveRecord::Base
has_many :property_operators
has_many :smart_group_properties, through: :property_operators
end
此帮助程序与此coffee脚本一起,成功地将多个智能组规则表单添加到页面中
现在的问题是:当在规则表单中选择属性值时,表单的其余部分需要使用所选属性的运算符进行更新。我使用上面的内联js,它使用jQuery.observe_字段插件来观察属性选择器。将调用控制器操作,并将所选属性的id作为属性id发送给控制器
class SmartGroupsController < ApplicationController
def property_selected
@property = SmartGroupProperty.find(params[:property_id])
@partial_name = @property.short + "_fields"
end
end
class SmartGroupsController
这允许设置一个变量@partial_name
然后,使用选定属性的操作符的分部更新视图(或“应”更新):
#property_selected.js.erb
$("#property-fields").html("<%= j render partial: @partial_name, locals: {f: f} %>");
#属性_selected.js.erb
$(“#属性字段”).html(“”);
…但我不知道如何将form builder对象发送到表单…或者是否可以使用Rails 4以这种方式处理它
我得到这个错误:
ActionView::Template::Error (undefined local variable or method `f' for #< <Class:0x007f9884088120>:0x007f98824c9e98>):
1: $("#property-fields").html("<%= j render partial: @partial_name, locals: {f: f} %>");
app/views/smart_groups/property_selected.js.erb:1:in _app_views_smart_groups_property_selected_js_erb__2922231718460238483_70146499765020'
ActionView::Template::Error(未定义的局部变量或#<:0x007f98824c9e98>的方法“f”):
1:$(“#属性字段”).html(“”);
app/views/smart_groups/property_selected.js.erb:1:in_app_views_smart_groups_property_selected_js_erb_2922231718460238483_70146499765020'
如何使表单的生成器对象从_rule_表单通过属性_selected操作传递到表单的更新部分?有没有更好的办法
感谢您提供的任何输入/指导。我认为,您不能在此部分中传递form builder变量。。。但你真的不需要。如果您只使用的字段_更好,这将创建一个单独的参数,您可以在控制器中使用该参数集
<%=fields_for 'properties' do |extra_fields| %>
<% ... %>
<% end %>
检查您的development.log在提交表单时传递了什么样的参数…我希望您使用动态表单生成gems。这些gem具有用于表单dom更改的js到事件观察者 你可以看看这块宝石。
ActionView::Template::Error (undefined local variable or method `f' for #< <Class:0x007f9884088120>:0x007f98824c9e98>):
1: $("#property-fields").html("<%= j render partial: @partial_name, locals: {f: f} %>");
app/views/smart_groups/property_selected.js.erb:1:in _app_views_smart_groups_property_selected_js_erb__2922231718460238483_70146499765020'
<%=fields_for 'properties' do |extra_fields| %>
<% ... %>
<% end %>