link_to_add_fields / unobtrusive javascript rails 4

时间:2014-05-21 08:22:23

标签: javascript jquery ruby-on-rails-4 nested-forms

我知道有关于railscast嵌套表单的一些线程......并且link_to_add_fields在rails 4中不再正常工作,因为有关不引人注目的js的更改。 有人可以帮助我理解并告诉我我必须改变什么才能使我的嵌套表格有效吗?我正在尝试“重建”:complex form但他使用的是铁轨3而我是铁轨4。 我确实收到以下错误:

undefined method `link_to_function' for
_form.html.erb中的

<%= link_to_add_fields("Add a Contact", f, :contacts, :class => "btn btn-primary", :title => "Add a new Contact") %>

我的application_helper.rb

def link_to_add_fields(name, f, association, options = {})
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{ association }", :onsubmit => "return $(this.)validate();") do |builder|
  render(association.to_s.singularize + "_fields", :f => builder)
end

link_to_function(name, "add_fields(this, \"#{ association }\", \"#{ escape_javascript(fields) }\")", options)

端 端

谢谢!

3 个答案:

答案 0 :(得分:4)

在rails 4.1中不推荐使用

link_to_function,建议使用不引人注目的JavaScript。

这就是我的建议:

使用提供的jQuery代码:

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}

在您的表单中:

<%= link_to_add_fields "Add a Contact", f, :contacts, "btn btn-primary", "Add a new Contact" %>

然后在application_helper:

def link_to_add_fields(name, f, association, cssClass, title)  
  new_object = f.object.class.reflect_on_association(association).klass.new  
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|  
    render(association.to_s.singularize + "_fields", :f => builder)  
  end  
  link_to name, "#", :onclick => h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"), :class => cssClass, :title => title 
end  

答案 1 :(得分:4)

我推荐了Mavis的代码 - 特别是link_to函数(忘了添加remote:true)

的application.js

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}

表格代码:

<%= link_to_add_fields "Add a Contact", f, :contacts, "btn btn-primary", "Add a new Contact" %>

这是对Mavis代码的唯一更改:

application_helper.rb

    def link_to_add_fields(name, f, association, cssClass, title)  
  new_object = f.object.class.reflect_on_association(association).klass.new  
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|  
    render(association.to_s.singularize + "_fields", :f => builder)  
  end  
  link_to name, "#", :onclick => h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"), class: cssClass, title: title, remote: true 
end  

答案 2 :(得分:0)

我假设你是从导演剧集http://railscasts.com/episodes/197-nested-model-form-part-2制作了这个帮手。

在您的application_helper.rb中,您已定义link_to_add_fields。可能是这样的。

def link_to_add_fields(name, f, association)

change it to this

def link_to_add_fields(name, f, association, locals={})

Then on the return statement.

link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", class: locals[:class])

最后,使用你的新方法

<%= link_to_add_fields "Add A Present", f, :presents, class: "btn btn-mini btn-info" %>