我如何在Rails3中实现父子关系 [英] how can i implement parent-child relationship in rails3

查看:97
本文介绍了我如何在Rails3中实现父子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用rails3创建一个计费应用程序,在创建计费时我很困惑.

I'm creating a billing application in rails3, I have a confusion when it comes to creating a bill.

一个帐单可以包含一个或多个项目

one bill can have one or more items

'bill' has many 'items'
'item' belongs to 'bill'

我的要求如下

我应该能够创建新帐单并向其添加项目(可以添加任意数量的项目)

I should be able to create a new bill and add items to it (any number of items can be added)

我的问题是

1-要使项目保存在bill_details表中,我首先应生成bill_id,什么是生成此Bill ID的最佳方法.

1 - to get items to be saved in the bill_details table I should first generate bill_id, What is the best way to generate this bill id.

2-实现这种情况的最佳方法是什么

2 - what is the best way to implement a scenario like this

3-我可以从Rails嵌套表单中获得任何帮助吗

3 - can i get any help from rails nested forms

谢谢

欢呼 sameera

cheers sameera

推荐答案

在这种情况下,您应该使用多态关联.您可以在此处实现此目的:

You should use polymorphic association in this scenario. Here what you can do to achieve this:

在bill.rb#模型文件中:

in bill.rb # model file:

class Bill < ActiveRecord::Base
  has_many :items  # establish association with items!
  # to save items in bill only if they are there!
  accepts_nested_attributes_for :items, :allow_destroy => :true,
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

在item.rb#模型文件中:

in item.rb # model file :

class Item < ActiveRecord::Base
      belongs_to :bill  # establish association with bill!
end

在您的bills_controller.rb中创建常规操作:index, new, create, show, edit, update, delete 进行更新操作:

In your bills_controller.rb create normal actions : index, new, create, show, edit, update, delete for update action:

def update
    @bill = Bill.find(params[:id])
    respond_to do |format|
      if @bill.update_attributes(params[:bill])
        format.html { redirect_to(@bill, :notice => 'Bill was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @bill.errors, :status => :unprocessable_entity }
      end
    end
  end

,您无需费心在items_controller.rb中创建任何action/方法,只需在view/Items/_form.html.erb中创建部分_form.html.erb并将其放置在此即可:

and you don't have to bother to create any action/ method in your items_controller.rb, just create a partial _form.html.erb in view/Items/_form.html.erb and put this:

    <%= form.fields_for :items do |item_form| %>
      <div class="field">
        <%= tag_form.label :name, 'Item:' %>
        <%= tag_form.text_field :name %>
      </div>
      <div class="field">
        <%= tag_form.label :quantity, 'Quantity:' %>
        <%= tag_form.text_field :quantity %>
      </div>
      <% unless item_form.object.nil? || item_form.object.new_record? %>
        <div class="field">
          <%= tag_form.label :_destroy, 'Remove:' %>
          <%= tag_form.check_box :_destroy %>
        </div>
      <% end %>
    <% end %>

现在,您必须从view/bills/_form.html.erb中调用它:

Now you have to call this from your view/bills/_form.html.erb:

<% @bill.tags.build %>
<%= form_for(@bill) do |bill_form| %>
  <% if @bill.errors.any? %>
  <div id="errorExplanation">
    <h2><%= pluralize(@bill.errors.count, "error") %> prohibited this bill from being saved:</h2>
    <ul>
    <% @bill.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>

  <div class="field">
    <%= bill_form.label :name %><br />
    <%= bill_form.text_field :name %>
  </div>

  <h2>Items</h2>
  <%= render :partial => 'items/form',
             :locals => {:form => bill_form} %>
  <div class="actions">
    <%= bill_form.submit %>
  </div>
<% end %>

View/Bills/new.html.erb:

View/Bills/new.html.erb:

<h1>New Bill</h1>

<%= render 'form' %>

<%= link_to 'Back', bills_path %>

View/Bills/edit.html.erb:

View/Bills/edit.html.erb:

<h1>Editing Bill</h1>

<%= render 'form' %>

<%= link_to 'Show', @bill %> |
<%= link_to 'Back', bills_path %>

关于, 苏里亚

这篇关于我如何在Rails3中实现父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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