2016-07-25 76 views
1

我有一個名爲「BillApp」的演習基本上它是有一些產品比爾,我應該可以使賬單,計算IVA等嵌套屬性無法在軌5,但在軌工作4

我有下面的模式:

create_table "bill_items", force: :cascade do |t| 
    t.integer "amount" 
    t.integer "product_id" 
    t.integer "bill_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.index ["bill_id"], name: "index_bill_items_on_bill_id" 
    t.index ["product_id"], name: "index_bill_items_on_product_id" 
    end 

    create_table "bills", force: :cascade do |t| 
    t.string "user_name" 
    t.string "dni" 
    t.date  "expiration" 
    t.float "sub_total" 
    t.float "grand_total" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "products", force: :cascade do |t| 
    t.string "name" 
    t.string "description" 
    t.float "price" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

比爾型號:

class Bill < ApplicationRecord 
    has_many :bill_items 
    has_many :products, through: :bill_items 
    accepts_nested_attributes_for :bill_items 
end 

BillItem型號:

class BillItem < ApplicationRecord 
    belongs_to :product 
    belongs_to :bill 
end 

產品型號:

class Product < ApplicationRecord 
    has_many :bill_items 
    has_many :bills, through: :bill_items 
end 

的ProductsController的是一個正常的,由支架產生的,它並不重要。

BillsController:

class BillsController < ApplicationController 
    before_action :set_bill, only: [:show, :update, :destroy, :edit] 

    def new 
    @bill = Bill.new 
    @bill.bill_items.build 
    end 

    def create 
    @bill = Bill.new(bill_params) 
    byebug 
    @bill.save 
    end 

    private 
    def set_bill 
     @bill = Bill.find(params[:id]) 
    end 

    def bill_params 
     params.require(:bill).permit(:user_name, :dni, { bill_items_attributes: [:product_id, :amount, :bill_id] }) 
    end 
end 

最後比爾新觀點:

<%= form_for(@bill) do |f| %> 
    <div> 
    <%= f.label :user_name %> 
    <%= f.text_field :user_name %> 
    </div> 
    <div> 
    <%= f.label :dni %> 
    <%= f.text_field :dni %> 
    </div> 

    <%= f.fields_for :bill_items do |fp| %> 
    <div> 
     <%= fp.label :product %> 
     <%= fp.collection_select :product_id, Product.all, :id, :name %> 
    </div> 
    <div> 
     <%= fp.label :amount %> 
     <%= fp.number_field :amount %> 
    </div> 
    <% end %> 

    <%= f.submit %></div> 
<% end %> 

的問題是非常具體的,在軌道5時,它試圖調用@ bill.save失敗,並在它顯示的錯誤:

#<ActiveModel::Errors:0x007fd9ea61ed58 @base=#<Bill id: nil, user_name: "asd", dni: "asd", expiration: nil, sub_total: nil, grand_total: nil, created_at: nil, updated_at: nil>, @messages={:"bill_items.bill"=>["must exist"]}, @details={"bill_items.bill"=>[{:error=>:blank}]}> 

但它在Rails 4.2.6中完美工作。 整個項目文件夾在這裏:https://github.com/TheSwash/bill_app currentrly在分支功能/ bills_controller

有人有關於發生了什麼的想法嗎?

+0

這一切說,在錯誤 - '@messages = {: 「bill_items.bill」= > [「must exist」]}',所以它與RoR5無關。 – Vucko

+0

@Vucko,我得到你,但這是在軌道4的問題相同的實現工作沒有這個錯誤,在這裏相同的代碼與Rails 4:https://github.com/TheSwash/bill_app_rails4 –

回答