在Rails 3中以嵌套模型形式动态添加字段 [英] Adding fields dynamically in a nested model form in Rails 3

查看:97
本文介绍了在Rails 3中以嵌套模型形式动态添加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观看了 http://railscasts.com/episodes/73-complex- forms-part-1 http://railscasts.com/episodes /74-complex-forms-part-2 ,但是在尝试代码时它似乎对我不起作用-自那时以来,我的假设是Rails内部发生了很多变化.第二个问题是我正在使用JQuery.

I watched http://railscasts.com/episodes/73-complex-forms-part-1 and http://railscasts.com/episodes/74-complex-forms-part-2 but it didn't seem to work for me when trying the code - my assumption is a lot has changed within rails since then. The second issue is I'm using JQuery.

有人知道在线上的任何教程都可能显示出更简单的方法吗?我已经制作了一个嵌套的模型表单-因此它实际上只是动态地添加/删除字段,多数民众赞成使我丧命.

Does anyone know of any tutorials online that might show an easier way to do this? I've already made one nested model form - so its really just the adding/removing fields dynamically part thats killing me.

推荐答案

这是一个简单的示例,显示了如何从单个页面发出多个邀请.缺少一些小细节,但可能足以提供帮助.您可以通过一些简单的jQuery从视图中添加和删除字段.该代码可以适用于任何类型的嵌套模型情况.希望能帮助到你! :)

Here's a simple example that shows how to send out multiple invites from single page. Some little details are missing but it might be enough to help. You can add and remove fields from the view via some simple jQuery. This code could be adapted to any kind of nested model situation. Hope it helps! :)

InviteController.rb

InviteController.rb

class InviteController < ApplicationController
  def new
    @invites = Invite.new
  end

  def create
    @invites = User.new(params[:user]).invites
    if @user.update_attributes(params[:user])
      return redirect_to root_url, :notice => "Your invite(s) were successfully sent!"
    else
      render :action => :new
    end
  end
end

User.rb

class User < ActiveRecord::Base
  has_many :invites

  accepts_nested_attributes_for :invites
end

Invite.rb

Invite.rb

class Invite < ActiveRecord::Base
  belongs_to :user
  after_create :send_invite

  private

  def send_invite
    # Send e-mail...
  end
end

new.html.erb

new.html.erb

<% form_tag invites_path do %>
  <%= error_messages_for :object => @user.invites %>
  <ul id="invite-list">
    <%= render @invites %>
  </ul>
  <div>
    <%= submit_tag "Send Invite" %>
    <%= link_to "Add Another", "#add", :id => "add-another" %>
  </div>
<% end %>

_invite.html.erb

_invite.html.erb

<%= fields_for "user[invites_attributes][]", invite do |i| %>
  <li>
    <%= link_to("Remove", "#delete", :class => "delete-invite") %>
    <%= i.label :full_name, "Full Name" %>
    <%= i.text_field :full_name %>
    <%= i.label :email, "Email Address" %>
    <%= i.text_field :email %>
  </li>
<% end %>

application.js

application.js

$(document).ready(function() {
  $('a#add-another').click(function() {
    $('#invite-list li:first').clone().find('input').val('')
    .end().appendTo('#invite-list');
  });

  $('.delete-invite').live('click', function() {
    if ($('#invite-list li').length > 1)
  $(this).parent().remove();
    else
  alert('You need at least one invite.')
  });
});

这篇关于在Rails 3中以嵌套模型形式动态添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆