最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - link_to_add_fieldsunobtrusive javascript rails 4 - Stack Overflow

programmeradmin1浏览0评论

i know there are some threads about the railscast nested forms... and that link_to_add_fields is not working proper in rails 4 anymore, because of the change about the unobstrusive js. can someone help me to understand and tell me what i do have to change in order to get my nested form working? i´m trying to "rebuild":plex form but he uses rails 3 and i´m rails 4. i do get the following error:

undefined method `link_to_function' for

in _form.html.erb:

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

my 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)

end end

thanks!

i know there are some threads about the railscast nested forms... and that link_to_add_fields is not working proper in rails 4 anymore, because of the change about the unobstrusive js. can someone help me to understand and tell me what i do have to change in order to get my nested form working? i´m trying to "rebuild":plex form but he uses rails 3 and i´m rails 4. i do get the following error:

undefined method `link_to_function' for

in _form.html.erb:

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

my 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)

end end

thanks!

Share Improve this question asked May 21, 2014 at 8:22 oliveroliver 1332 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

link_to_function is deprecated in rails 4.1 and it is remended the use of Unobtrusive JavaScript instead.

this is what I'd suggest:

use the jQuery code provided:

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));  
}

in your form:

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

then in 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  

I tweeked Mavis's code - specifically the link_to function (forgot to add 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));  
}

Form code:

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

This is the only change to Mavis's code:

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  

I am assuming you made this helper from the rails cast episode http://railscasts./episodes/197-nested-model-form-part-2.

In your application_helper.rb, you have defined link_to_add_fields. Probably something like this.

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])

Finally, use your new method like so

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

评论列表(0)

  1. 暂无评论