Ruby on rails 3 嵌套表单字段:价格,重复表单几次
我有很多对很多的关系,然后我就这样做了Ruby on rails 3 嵌套表单字段:价格,重复表单几次,ruby-on-rails-3,nested-forms,nested-attributes,Ruby On Rails 3,Nested Forms,Nested Attributes,我有很多对很多的关系,然后我就这样做了 fields_for :device 这显示在良好的方式,但我不能保存它我得到未知属性:价格 和字段\u:设备 以此类推,如果我写f.fields\u for:price它给出了很好的文本字段计数,但它写 unknown attribute: price Rails.root: C:/Users/Ignas/mildai/baze4 Application Trace | Framework Trace | Full Trace app/contr
fields_for :device
这显示在良好的方式,但我不能保存它我得到未知属性:价格
和字段\u:设备
以此类推,如果我写f.fields\u for:price
它给出了很好的文本字段计数,但它写
unknown attribute: price
Rails.root: C:/Users/Ignas/mildai/baze4
Application Trace | Framework Trace | Full Trace
app/controllers/commercial_offers_controller.rb:74:in `block in update'
app/controllers/commercial_offers_controller.rb:73:in `update'
多谢各位
其他信息:
控制器
def edit_prices
@commercial_offer = CommercialOffer.find(params[:id])
end
链接以编辑价格
<%= link_to "Edit prices", :controller => "CommercialOffers", :action => "edit_prices", :id => @commercial_offer %>
“CommercialOffers”,:action=>“编辑价格”,:id=>@commercial\u offer%>
_编辑_price.html.erb
<%= form_for @commercial_offer do |f| %>
<% CommercialOffer.find(params[:id]).devices.each do |device| %>
<%= check_box_tag "commercial_offer[device_ids][]", device.id, @commercial_offer.devices.include?(device) %>
<%= device.name %>
<%= f.fields_for :prices do |builder| %>
<%= render 'prices/new_price', :f => builder, :commercial_offer => @commercial_offer, :device => device %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit "Save"%>
</div>
<% end %>
建筑商,:commercial\u offer=>@commercial\u offer,:device=>device%>
对于一个设备,它只渲染一次,但对于一个设备,它渲染的次数是具有相同商业id的设备的多少倍
_new_price.html.erb
Price:
<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id, :value => device.id %></br>
class CommercialOffer < ActiveRecord::Base
has_many :prices
has_many :devices, :through => :prices
accepts_nested_attributes_for :prices
accepts_nested_attributes_for :devices
end
class Device < ActiveRecord::Base
has_many :prices
accepts_nested_attributes_for :prices
has_many :commercial_offer, :through => :prices
class Price < ActiveRecord::Base
belongs_to :device
belongs_to :commercial_offer
end
\u new\u price.html.erb
价格:
数量:
设备id:
设备id%>
class CommercialOffer:价格
接受\u嵌套的\u属性\u:价格
接受设备的\u嵌套\u属性\u
终止
类设备:价格
类价格
方法没有任何字段
你的问题也不清楚。你到底想做什么?只显示一个设备?哪一个?首先,最后
UPD
商业报价.rb:
class CommercialOffer < ActiveRecord::Base
has_many :prices
has_many :devices, :through => :prices
accepts_nested_attributes_for :prices
accepts_nested_attributes_for :devices, :allow_destroy => true
end
class CommercialOffer:价格
接受\u嵌套的\u属性\u:价格
接受\u嵌套的\u属性\u for:devices,:allow\u destroy=>true
终止
你的观点
<%= form_for @commercial_offer do |f| %>
<%= f.fields_for :devices do |device| %>
<p>
<%= device.check_box :_destroy %>
<%= device.label "Destroy?" %>
</p>
<p>
<%= device.text_field :name %>
</p>
<%= device.fields_for :prices do |builder| %>
<%= render 'prices/new_price', :f => device %>
<% end %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit "Save"%>
</div>
<% end %>
设备%>
_新价格部分
<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id %></br>
数量:
设备id:
是的,我犯了一个错误。我有3个表,第一个是带有字段名的商业报价,第二个是带有字段名的设备,只有很少的字段,如名称、说明和最后一个带有商业报价id、设备id和价格字段的价格,我选择了一些设备进行商业报价,它们记录在价格表中,所以我想为所有关联添加价格,它几乎可以正常工作,但编辑视图中的记录是乘法的,如果我在商业报价中有2台设备,我会看到4条记录,如果我有3台,我会看到9条记录,但当我使用fields_for:price时,它会给我很好的字段数,但它无法保存itok show your models和show your All view。但是我仍然不理解你未定义的方法“check_box”,它以相同的方式呈现,对于一个设备,它多次使用设备。我忘记了在那里使用等号=