嵌套表单 fields_for :价格,重复表单几次

发布于 2024-11-03 14:28:46 字数 2442 浏览 3 评论 0原文

我有多对多的关联,然后我

fields_for :device

以良好的方式进行此显示,但我无法保存它,我得到未知的属性:价格 fields_for :device

fields_for :devices

field_for :devices

依此类推,一台设备会再重复一次,如果我写 f.fields_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 %>

_edit_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_offer_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

i have many to many throught asociation and then i do this

fields_for :device

this displaying in good way, but i cannot save it i get unknown attribute :price
fields_for :device

And fields_for :devices

field_for :devices

And so on, one device makes one more repeat, if i write f.fields_for :price it gives good text field count, but it write

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'

thank you

additional information:

controller

 def edit_prices
        @commercial_offer = CommercialOffer.find(params[:id])
     end

link to edit prices

  <%= link_to "Edit prices", :controller => "CommercialOffers", :action => "edit_prices", :id => @commercial_offer %>

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

for one device it have render only one time, but it rendering for one device such times how many devices is with same commercial_offer_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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-11-10 14:28:46

没有任何 field_for 方法。

另外你的问题也不清楚。你到底想做什么?只显示一台设备?哪一个?第一个,最后一个?

UPD

Commercial_offer.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

您的视图

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

_new_price 部分

<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id %></br>

There is no any field_for method.

Also your question isn't clear. What actually you want to do? Show only one device? Wich one? First, last?

UPD

commercial_offer.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

Your view

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

_new_price partial

<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id %></br>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文