Rails 3.1:添加&使用动态jQuery删除字段(Railscasts#197)

时间:2011-08-01 06:44:33

标签: ruby-on-rails nested-forms

Rails noob在这里 我一直在Google和StackOverflow上搜索信息,以便在Railscasts #197中使用示例,但我访问过的链接都没有与Rails 3.1一起使用!
当我点击删除或添加新字段按钮时,绝对没有任何反应。这太令人沮丧了:/

任何人都知道为什么我在下面提供的代码不像RailsCasts一集那样动态添加字段或删除字段?

application_helper.rb

def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end 

def link_to_add_fields(name, f, association)
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_function(name, "add_fields(this, '#{association}', '#    {escape_javascript(fields)}')", :remote => true)
 end



app.js

// delete characters on users#edit and users#new
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest("#character").hide();
}

// add character fields on users#edit and users#new
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));  
}



HTML

<div id="account">
<div class="field">
    <%= f.label :account_name %><br />
    <%= f.text_field :account_name %>
</div>
<div class="field">
    <%= f.radio_button(:realm, "USWest") %>
    <%= f.label(:realm, "USWest") %>

    <%= f.radio_button(:realm, "USEast") %>
    <%= f.label(:realm, "USEast") %>

    <%= f.radio_button(:realm, "Europe") %>
    <%= f.label(:realm, "Europe") %>

    <%= f.radio_button(:realm, "Asia") %>
    <%= f.label(:realm, "Asia") %>
</div>      
<div class="field">
    <%= link_to_add_fields "Add new account", f, :characters %>
</div>

<div class="field"> 
    <%= f.hidden_field :_destroy %>
     <%= link_to_remove_fields "remove", f %>
</div>
<%= f.fields_for :characters do |builder| %>
<%= render "characters/char_fields", :f => builder %>
<% end %>
</div>

2 个答案:

答案 0 :(得分:1)

我看不到必要的部分叫:

render(association.to_s.singularize + "_fields", :f => builder)

答案 1 :(得分:1)

好的,首先你需要确保在application.html.erb文件的头部为application.js文件使用正确的rails 3.1 include标记。在屏幕演员中,他使用旧的方式:

<%= javascript_include_tag :defaults, :cache => true %>

新方法是:

<%= javascript_include_tag "application" %>

现在在所述application.js文件中,您现在将看到下面的代码,不要删除它,在资产管道中使用注释来包含jquery,它现在捆绑了rails而不是prototype.js。

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

看起来您的代码已经设置为jquery了,如果您想轻松将该代码附加到评论下的application.js文件的底部,您应该设置。