【问题标题】:Rails 3 content_for issuesRails 3 content_for 问题
【发布时间】:2010-11-01 22:46:20
【问题描述】:

我最近使用 http://github.com/timriley/complex-form-examples 构建了一个相当深的嵌套表单作为指导。表单部分使用以下内容,以便在单击链接时呈现新字段:

<%= yield :warehouses_fields_template %>
<%= yield :ratgrades_fields_template %>

这些内容在 ApplicationHelper 中生成,如下所示:

def new_child_fields_template(form_builder, association, options = {})
    content_for "#{association}_fields_template" do
      options[:object] ||= form_builder.object.class.reflect_on_association(association).klass.new
      options[:partial] ||= association.to_s.singularize
      options[:form_builder_local] ||= :f

      content_tag(:div, :id => "#{association}_fields_template", :style => "display: none") do
        form_builder.fields_for(association, options[:object], :child_index => "new_#{association}") do |f|
          render(:partial => options[:partial], :locals => {options[:form_builder_local] => f})
        end
      end
    end unless content_given?("#{association}_fields_template")
end

def content_given?(name)
    content = instance_variable_get("@content_for_#{name}")
    ! content.nil?
end

一切似乎都在 rails 2.3.8 上运行良好,但今天升级到 Rails 3 rc 后,模板不再加载。有什么改变会使上面的代码无效吗?其他人注意到同样的问题吗?

任何帮助将不胜感激, 谢谢!

作为参考,这也是相关的 jQuery 代码:

$(function() {
  $('form a.add_child').live('click', function() {
    // Setup
    var assoc   = $(this).attr('data-association');           // Name of child
    var content = $('#' + assoc + '_fields_template').html(); // Fields template

    // Make the context correct by replacing new_<parents> with the generated ID
    // of each of the parent objects
    var context = ($(this).parents('.fields').children('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');

    // context will be something like this for a brand new form:
    // project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
    // or for an edit form:
    // project[tasks_attributes][0][assignments_attributes][1]
    if(context) {
      var parent_names = context.match(/[a-z]+_attributes/g) || []
      var parent_ids   = context.match(/[0-9]+/g)

      for(i = 0; i < parent_names.length; i++) {
        if(parent_ids[i]) {
          content = content.replace(
            new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
            '$1[' + parent_ids[i] + ']'
          )
        }
      }
    }

    // Make a unique ID for the new child 
    var regexp  = new RegExp('new_' + assoc, 'g');
    var new_id  = new Date().getTime();
    content     = content.replace(regexp, new_id)

    $(this).parent().before(content);
    return false;
  });
});

如果有帮助的话,我已经在更多相关页面中包含了一个要点:http://gist.github.com/536704

【问题讨论】:

  • 请编辑您的问题并格式化您的代码以使其可读。

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

遇到了同样的问题。在 rails 3 中,您现在必须使用符号。所以使用:

content_for :"#{association}_fields_template" do

end unless content_for?(:"#{association}_fields_template")

(删除无用的content_given?方法,现在是内置的content_for?)。

也用

<%= content_for :warehouses_fields_template %>
<%= content_for :ratgrades_fields_template %>

而不是

<%= yield :warehouses_fields_template %>
<%= yield :ratgrades_fields_template %>

【讨论】:

  • 谢谢!当我遇到同样的事情时解决了我的问题。谢谢!!
猜你喜欢
  • 2011-04-06
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 2011-12-02
  • 2011-08-20
  • 2022-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多