2016-07-25 69 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 –

回答