Rails 3 and jquery railscast 197 releases

Im doing railscast episode 196-197, everything works fine, except that there is no "add answers", "add a question", just "delete a question", Im works with ruby1.9.2 and rails 3.1.1, I know that the episode is in rails 2.3, but it should be something like = or%, which I missed ... at the end of the episode give an option for jquery but it doesn’t work for me, please help! thanks in advance.

0
source share
2 answers

Thanks DeathHammer. This has been fixed for me.

These were syntax changes in Rails 3 for the helper method that did this.

Railscast ex:

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

Your working ex:

link_to_function(name, ("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
+1
source

, , .   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)}')"))
  end

application.js

function remove_fields (link) {
  $(link).prev("input[type=hidden]:first").val('1');
    $(link).closest(".fields").hide();

}

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

}

:

<%= f.fields_for :uploads do |upload| %>
    <div class="fields">
      Upload Photo: <%=upload.file_field :photo %>
    </div>
<% end %>
<p> <%= link_to_add_fields 'Need to upload More files? Please click here.', f, :uploads %> </p>

_modelname_fields.html.erb, ( ),

<div class="fields">
  Upload Photo: <%=f.file_field :photo %>
</div>
<br />

.

+1

Source: https://habr.com/ru/post/1750608/


All Articles