2014-05-21 2 views
3

railscast 중첩 된 폼에 대한 스레드가 있다는 것을 알고 있습니다. 링크가없는 add_fields는 불안정한 j에 대한 변경 때문에 더 이상 레일 4에서 제대로 작동하지 않습니다. 내 중첩 된 양식을 작동시키기 위해 내가 변경해야하는 것을 이해하고 알려주는 사람이 있습니까? "다시"하려고 미안 : complex form을하지만 그는 레일 3 미안 레일을 사용 4. 나는 다음과 같은 오류받을 수 있나요 :link_to_add_fields/눈에 잘 띄지 않는 javascript 레일 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) 

끝 끝

일 anks!

답변

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" %> 
+0

감사. 나는이 시도했지만 오류 (정의되지 않은 메서드 ...) 여전히 나타나고 ... 전체 줄을 밖으로 걸릴 경우 양식이 될 것 같아 보여주고있다. 레일 3.2.9에서 전혀 문제없이 작동합니다 ... – oliver

4

link_to_function 레일 4.1에서 사용되지 않으며 대신 겸손한 자바 스크립트의 사용을 권장처럼 새로운 방법을 사용합니다. application_helper에서 다음

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

:

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 
4
양식에

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

:

제공하는 jQuery 코드를 사용

내가 좋을 것 것입니다

나는 Mavis의 코드를 tweeked - sp ecifically LINK_TO 기능 (원격 추가하는 것을 잊었다 : 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" %> 

이 메이 비스의 코드에 대한 유일한 변화 :

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