Rails:模型接受嵌套属性错误

时间:2013-11-23 00:19:33

标签: ruby-on-rails-3

在此花了大约6个小时并在这里看了很多类似的问题我虽然在生气之前我最好问一下

我似乎可以让这个nested_attributes工作

我一直在

  

未初始化的常量Order :: OrderLine

orders.rb

class Order < ActiveRecord::Base

  attr_accessible :customer_id, :integer, :job_number, :net_price, :order_number, :total_price, :vat,
            :account_id, :delivery_address, :customer_name, :status, :order_lines_attributes

  belongs_to :customer
  has_many :order_lines
  accepts_nested_attributes_for :order_lines, allow_destroy: true
  before_save :createJobNumber

    def createJobNumber
        self.job_number = rand(1..999)
    end

    def status
        self.status == "Draft Quotation" unless self.status
    end

    def customer_name

    end

    def customer_name=(name)

    end


end

order_lines.rb

    class OrderLines < ActiveRecord::Base

        belongs_to :order
        has_many :products
        attr_accessible :account_id, :comments, :order_id, :product_id, :quatity

    end

这是视图

<div class="row">
  <%= form_for @order do |f| %>
  <% if @order.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>

    <ul>
      <% @order.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
  <% end %>

  <div class="col-md-5 col-md-offset-1">
        <div class="control-group">
          <div class="controls required">
            <%= f.hidden_field :customer_id, :class => 'number_field', :id =>"cust_id" %>
            <%= f.autocomplete_field :customer_name, autocomplete_customer_name_customers_path, :id_element => '#cust_id', :class => 'text_field ui-autocomplete-input', :placeholder => 'Customer Name' , :update_elements => {} %>
          </div>
        </div>
        <div class="control-group" >
          <div class="controls" >
            <%= f.text_field :delivery_address, :class => 'address-picker-input', :placeholder =>'Delivery Address' %>
          </div>
        </div>


  </div>
  <div class="col-md-6">
          <div class="control-group">
              <%= f.text_field :order_number, :placeholder => 'Order number' %>
          </div>
  </div>
</div> <!-- close row -->
<div class="">
  <table class="table border">
      <thead class = "silver border">
          <tr>
            <th>Quantity</th>
            <th>Product</th>
            <th>Rate</th>
            <th>Comments</th>
            <th></th>

        </tr>
    </thead>
    <%= f.fields_for :order_lines do |builder| %>
    <tr>
      <td><%= builder.text_field :quatity, :placeholder => 'Qty' %></td>
      <td><%= builder.text_field :product_id, :placeholder => 'Product' %></td>
      <td><%= builder.text_field :rate, :placeholder => 'Rate' %></td>
      <td><%= builder.text_field :comments, :placeholder => 'Comments' %></td>
      <td></td>
  </tr>
    <% end %>
    </table>
  </div>


  <div class="actions">
    <%= f.submit nil, :class=>'btn btn-success' %>
  </div>
  <% end %> 
    <div class="modal fade" id="new_modal">
  </div>

最后是orders_controller.rb

class OrdersController < InheritedResources::Base

    def new
        @order = Order.new
        @order.order_lines.build

        respond_to do |format|
            format.html # show.html.erb
        end
    end
end

1 个答案:

答案 0 :(得分:0)

Rails正在寻找单一OrderLine的模型名称,但您的模型被命名为OrderLines。要解决此问题,请将模型重命名为singular(OrderLine)。