2012-04-30 48 views
2

我甲肝这在我的store_opening_stock形式更新2款一次与Ruby on Rails中相同的属性

<%= 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个型号,一个是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 

有没有办法可以更新产品的价格,PRODUCT_ID,数量,批次号从store_opening_stocks尽快股票作为我点击提交在store_opening_stock形式?????我可以一次更新2个表格,因为我在轨道上按下提交Ruby? 在此先感谢

回答

2

是的。看到您已经在stock.rb模型中使用'accep_nested_attributes_for',这是一个很好的做法,同时从库存模型表单中保存多个'store_opening_stock'记录。

但是,如果你想在不同形式的模型属性,您可以通过使用fields_for

+0

的NiMesh做到这一点我m是一有点困惑。抱歉,我对这个框架感兴趣,请问field的for如何帮助我更新这两个表中的'price',同时使用一种形式? – bharath

+0

请参考上面的'fields_for'文档链接,并向下滚动到'一对多'部分,这是与您的要求最相似的部分。一旦你插入“股票”模型的价格输入字段表单,它们将自动在你的数据库保存store_opening_stock当创建 –

+0

经历了,非常感谢的NiMesh。 – bharath