Rails 3.1.1 функция escape_javacript отображает обычный текст вместо добавления элемента HTML

#jquery #ruby-on-rails #nested-forms

#jquery #ruby-on-rails #вложенные формы

Вопрос:

То, что я пытаюсь сделать, это динамически добавлять фрагменты формы внутри вложенной формы в rails 3.1.1. У меня есть этот javascript внутри CDATA, сгенерированный из вспомогательного метода rails, который использует метод escape_javascript. После вызова link_to ‘Add Ajax’, как показано ниже, я получаю обычный текст вместо добавляемых полей ввода HTML. Что мне действительно нравится, так это создание полей ввода, а не простого мусорного текста. Пожалуйста, помогите.

 <script type="text/javascript">
//<![CDATA[

          var mappings='amp;<p class=amp;quot;fieldsamp;quot;amp;>n    amp;<input id=amp;quot;resource_synchronization_attributes_mappings_attributes_NEW_RECORD_sourceamp;quot; name=amp;quot;resource[synchronization_attributes][mappings_attributes][NEW_RECORD][source]amp;quot; size=amp;quot;30amp;quot; type=amp;quot;textamp;quot; /amp;>n    amp;<input id=amp;quot;resource_synchronization_attributes_mappings_attributes_NEW_RECORD__destroyamp;quot; name=amp;quot;resource[synchronization_attributes][mappings_attributes][NEW_RECORD][_destroy]amp;quot; type=amp;quot;hiddenamp;quot; value=amp;quot;falseamp;quot; /amp;>amp;<a href=amp;quot;#amp;quot; onclick=amp;quot;remove_fields(this); return false;amp;quot;amp;>removeamp;</aamp;>namp;</pamp;>nnnn'


//]]>
  

custom.js

 replace_ids = function(s) {
  var new_id = new Date().getTime()
  return s.replace(/NEW_RECORD/g, new_id)
}

$(document).ready(function() {
  $(".add_nested_item").click(function() {
    var template = eval($(this).attr("href").replace(/.*#/, ''))
    $($(this).attr("rel")).append(replace_ids(template))
    return false
  })
})
  

application_helper.rb

   def generate_html(form_builder, method, options = {})
    options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
    options[:partial] ||= method.to_s.singularize
    options[:form_builder_local] ||= :f

    form_builder.fields_for(method, options[:object], :child_index => "NEW_RECORD") do |f|
      render(:partial => options[:partial], :locals => { options[:form_builder_local] => f })
    end
  end

  def generate_template(form_builder, method, options = {})
    escape_javascript generate_html(form_builder, method, options)
  end
  

_form.html.erb

 <div class="fields">
<p>
  <%= f.label :start_datetime, "Start On" %><br />
  <%= f.date_select :start_datetime %><br />

</p>

    <% content_for :java do %>
        <%= "var mappings='#{generate_template(f, :mappings)}'" %>
    <% end %>

    <div id="mappings">
        <%= f.fields_for :mappings do |builder| %>
          <%= render 'mapping', :f => builder %>
        <% end %>

    </div>
  <%= link_to 'Add Ajax', '#mappings', :class => 'add_nested_item', :rel => '#mappings' %>


</div>
  

Модели

 class Resource < ActiveRecord::Base

  has_many :utilities
  has_many :people, :through => :utilities

  has_many :person_attributes

  has_one :synchronization
  accepts_nested_attributes_for :synchronization

  validates_presence_of :name


end



class Synchronization < ActiveRecord::Base
  belongs_to :resource
  has_many :mappings
  accepts_nested_attributes_for :mappings,  :reject_if => lambda { |a| a[:source].blank? }, :allow_destroy => true

  #validates_presence_of :start_datetime
end


class Mapping < ActiveRecord::Base
  belongs_to :synchronization
  has_many :data, :class_name => 'PersonAttribute'
  has_many :people, :through => :data

end