2

これは store_opening_stock フォームにあります

    <%= simple_form_for @store_opening_stock do |f| %>
      <%= f.error_messages %>
      <p>
      <%= f.hidden_field :user_id, :value => current_user.id %>

      <%= f.label :employee_id %>
      <%= f.collection_select :employee_id, Employee.all, :id, :name, options ={:prompt => "-Select  employee  Name"}, :class => "employee name", :style=>'width:210px;'%><br />

         <%= f.label :date %>
        <%= f.datepicker :date, :dateFormat => 'dd MM, yy',  :showOn => "both", :buttonImage => "/assets/calendar.gif", :buttonImageOnly => true, :changeMonth => true, :changeYear => true, :placeholder => "Click on the calender", :yearRange =>'1950:2042' %><br/>

        <%= f.label :product_id %>
        <%= f.collection_select :product_id, Product.all, :id, :title, options ={:prompt => "-Select  Product  Name"}, :class => "product name", :style=>'width:210px;'%><br /> 

        <%= f.input :batch_no,  :label => 'Batch No', :input_html => { :size => 30}%><br/>
        <%= f.input :price,  :label => 'price', :input_html => { :size => 30}, :placeholder =>'Enter Digits'%><br/> 
        <%= f.input :quantity,  :label => 'quantity', :input_html => { :size => 30}, :placeholder =>'Enter Digits'%><br/>
       <p><%= f.submit %></p>
<% end %>

そして、私は2つのモデルを持っています.1つはstore_opening_stock.rbとstock.rbです

class StoreOpeningStock < ActiveRecord::Base
    attr_accessible :comments_attributes, :stocks_attributes
  attr_accessible :user_id, :date ,:store_location_id, :product_id, :batch_no
  attr_accessible :expiry_date, :price, :quantity, :comment_id, :employee_id
  attr_accessible :goods_receipt_id

  has_many :stocks    
end


class Stock < ActiveRecord::Base
    attr_accessible :date, :store_location_id, :product_id, :batch_no, :expiry_date, :price, :quantity, :goods_reciept_id

    belongs_to :store_opening_stock, :foriegn_key => "store_opening_stock_id"
    acts_as_store_opening_stock
    accepts_nested_attributes_for :store_opening_stocks
end
end

store_opening_stock フォームで送信をクリックするとすぐに、PRICE、PRODUCT_ID、QUANTITY、BATCH-NO を store_opening_stocks から在庫に更新する方法はありますか????? Ruby on railsでsubmitを押すと一度に2つのテーブルを更新できますか???? 前もって感謝します

4

1 に答える 1

2

はい。これは、ストック モデル フォームから複数の「store_opening_stock」レコードを保存する際の優れた方法です。

しかし、フォームに異なるモデル属性を持たせたい場合は、fields_forを使用してこれを行うことができます

于 2012-04-30T10:16:26.467 に答える