2016-07-25 19 views
1

Ich habe eine Übung namens „BillApp“ es ist im Grunde ein Gesetz, dass einige Produkte haben, und ich sollte könnte Rechnungen machen, berechnen IVA usw.Nested Attribute nicht in Schienen 5, arbeitet aber in Schienen 4

Ich habe das nächste Schema:

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 

Bill Modell:

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

BillItem Modell:

class BillItem < ApplicationRecord 
    belongs_to :product 
    belongs_to :bill 
end 

Produktmodell:

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

Die Products ist ein normaler, von Gerüst erzeugt, es spielt keine Rolle.

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 

Und schließlich die Bill neue Ansicht:

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

Das Problem ist sehr spezifisch, in Schienen 5, wenn es versucht @ zu nennen bill.save es und in der versagt Fehler zeigt es:

#<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}]}> 

Aber es funktioniert perfekt in Rails 4.2.6. Der gesamte Projektordner ist hier: https://github.com/TheSwash/bill_app currentrly in der Branche Merkmale/bills_controller

Jemand eine Idee hat, was geschieht?

+0

Alles sagt im Fehler - '@messages = {: "bill_items.bill"= > ["muss existieren"]} ', also hat es nichts mit RoR5 zu tun. – Vucko

+0

@Vucko, ich bekomme dich, aber das ist die Frage in Schienen 4 die gleiche Implementierung funktioniert ohne diesen Fehler, hier der gleiche Code mit Rails 4: https://github.com/TheSwash/bill_app_rails4 –

Antwort