开发者

ruby, rails, railscasts example I messed up

开发者 https://www.devze.com 2022-12-26 14:39 出处:网络
If you saw the railscasts on nested forms this is the helper method to create links dynamically. However, after I upgraded to ruby 1.9.2 and rails 3 this doesn\'t work and I have now idea why.

If you saw the railscasts on nested forms this is the helper method to create links dynamically. However, after I upgraded to ruby 1.9.2 and rails 3 this doesn't work and I have now idea why.

    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, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end

here is the javascript

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

}

When I view source this is how the link is getting rendered:

<p><a href="#" onclick="add_fields(this, &quot;dimensions&quot;, &quot;&quot;); return false;">Add Dimension</a></p>

so &quot;&quot; is not the correct information to build a new template and something is going on with how the string for fields is getting set. such as fields= f.fields_for


It appears f.fields_for no longer returns the string of rendered fields. I'm guessing this is a side effect from the ERB Blocks change in which case it would only apply to Rails 3 Beta 2. I consider this a bug, and hopefully it will be fixed before the final release, but for now you can do a quick fix like this:

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


I am too implementing Ryan's Nested Forms into a Rails 3 (Ruby 1.9.2) app, but am faced with the following error:

undefined method `klass' for nil:NilClass

in terms of line 2 of the following helper method:

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|
        safe_concat(render(association.to_s.singularize + "_fields", :f => builder))
    end
    link_to_function(name, ("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end

Does anyone know what may be causing this?

I thought maybe it related to the version of ruby I was using (i.e. I was originally using REE, but have now switched to 1.9.2), but I still receive the same problem.

Many thanks for any help!


In Rails 3.0.9 you can fix this by replacing this code

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

through this

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


If you feel its due to the upgrade to rails ver 3 then do a gem list If you have any older rails versions installed then type this:

RAILS_GEM_VERSION = '(Your older rails version here)' unless defined? RAILS_GEM_VERSION

in the environment.rb file in your config folder. Its also good to freeze your rails version for your app.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号