how can i implement parent-child relationship in rails3

非 Y 不嫁゛ 提交于 2020-01-06 07:11:43

问题


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'

my requirement is as follows

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

my problem is

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 - what is the best way to implement a scenario like this

3 - can i get any help from rails nested forms

thanks

cheers sameera


回答1:


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

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

in item.rb # model file :

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

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

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 %>

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:

<h1>New Bill</h1>

<%= render 'form' %>

<%= link_to 'Back', bills_path %>

View/Bills/edit.html.erb:

<h1>Editing Bill</h1>

<%= render 'form' %>

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

Regards, Surya



来源:https://stackoverflow.com/questions/5708160/how-can-i-implement-parent-child-relationship-in-rails3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!