简体   繁体   中英

Dynamic form fields in Rails3 with jquery

I am using rialscasts #74 as a guide.

I am trying to dynamically add form fields via a text-link. In the railscast episode he achieves it very nicely using the following code:

<!-- layouts/application.rhtml -->
<%= javascript_include_tag :defaults %>

<!-- projects/new.rhtml -->
<div id="tasks">
  <%= render :partial => 'task', :collection => @project.tasks %>
</div>
<p><%= add_task_link "Add a task" %></p>

<!-- projects/_task.rhtml -->
<div class="task">
<% fields_for "project[task_attributes][]", task do |task_form| %>
  <p>
    Task: <%= task_form.text_field :name %>
    <%= link_to_function "remove", "$(this).up('.task').remove()" %>
  </p>
<% end %>
</div>

 # projects_helper.rb

 def add_task_link(name)
    link_to_function name do |page|
      page.insert_html :bottom, :tasks, :partial => 'task', :object => Task.new
    end
 end

content within projects_help.rb is what I am most interested in. The problem is he is doing this via prototype. I am looking for an exact duplicate implementation using jquery (and rails3). What do you think? Thanks!

ya I was going to say -- that is a very old railscast and is using both outdated practices and an old framework. I'm gonna try to help you out.

layouts/application.erb

<%= javascript_include_tag :defaults %>

projects/new.erb

<div id="tasks">
  <%= render :partial => 'task', :collection => @project.tasks %>
</div>
<p><%= add_task_link "Add a task" %></p>

projects/_task.erb

<div class="task">
<!-- I think this part should still work -->
<%= fields_for "project[task_attributes][]", task do |task_form| %>
  <p>
    Task: <%= task_form.text_field :name %>
    <!-- We're going to be defining the click behavior with unobtrusive jQuery -->
    <%= link_to "remove", "#", :class => 'remove_task'
  </p>
<% end %>
</div>

projects_helper.rb

def add_task_link(name)
  # This is a trick I picked up... if you don't feel like installing a
  # javascript templating engine, or something like Jammit, but still want
  # to render more than very simple html using JS, you can render a partial
  # into one of the data-attributes on the element.
  #
  # Using Jammit's JST abilities may be worthwhile if it's something you do
  # frequently though.
  link_to name, "#", "data-partial" => h(render(:partial => 'task', :object => Task.new)), :class => 'add_task'
end

public/javascripts/application.js

$(function(){
  // Binds to the remove task link...
  $('.remove_task').live('click', function(e){
    e.preventDefault();
    $(this).parents('.task').remove();
  });

  // Add task link, note that the content we're appending
  // to the tasks list comes straight out of the data-partial
  // attribute that we defined in the link itself.
  $('.add_task').live('click', function(e){
    e.preventDefault();
    $('#tasks').append($(this).data('partial'));
  });
});

The current RailsCasts/ASCIICasts are #196 (see http://asciicasts.com/episodes/196-nested-model-form-part-1 ) & 197 which have been updated for Rails3.

Ryan Bates has also created a gem called nested_form (see https://github.com/ryanb/nested_form ) which handles much of this for you. He has has also created examples of complex nested forms using Prototype, jQuery, and his nested form gem; you can find a link on the nested_forms page.

Good stuff!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM