2017-05-29 3 views
1

Ich versuche, eine Verbindung zwischen Printlist und Produkt, Vorlage und Papiertyp herzustellen, aber ich bekomme einen Fehler. Wenn ich das neue Formular einreiche, wird es auf die Indexroute umgeleitet, aber es wird auf der gleichen neuen Seite fortgesetzt und die Informationen werden nicht in der Datenbank gespeichert.Rails speichern nicht Formular in die DB

Controller:

class PrintlistsController < ApplicationController 
    before_action :set_printlist, only: [:show, :edit, :update, :destroy] 

    def index 
    @printlists = Printlist.all 

    @preco = calcular_preco(Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id)) 
    end 

    def show 
    end 

    def new 
    @printlist = Printlist.new 
    end 

    def edit 
    end 

    def create 
    #binding.pry 
    #product = Product.where('codref': product_params['codref']) 
    #template = Poster.find(layout_params['id']) 
    #size = Cartaze.find(cartaz_params['id']) 

    #if product.nil? 
    # product = Product.new 
    # product.codref = product_params['codref'] 
    # product.title = product_params['title'] 
    # product.tipo = product_params['tipo'] 
    # product.size = product_params['size'] 
    # product.save 
    #end 
    @printlist = Printlist.new(printlist_params) 
    #@printlist.product = product 
    #@printlist.template = template 
    #@printlist.size = size 

    respond_to do |format| 
     if @printlist.save 
     format.html { redirect_to printlists_path , notice: 'Impression was successfully created.' } 
     format.json { render :show, status: :created, location: @printlist } 
     else 
     format.html{ render :new } 
     format.json{ render json: @printlistst.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @printlist.update(printlist_params) 
     format.html { render action: "edit" } 
     format.json { render :show, status: :ok, location: @printlist } 
     else 
     format.html { render :edit } 
     format.json { render json: @printlist.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @printlist = Printlist.find(params[:id]) 
    @printlist.destroy 

    respond_to do |format| 
     format.html { redirect_to(Printlists_path) } 
     format.xml { head :ok } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_printlist 
     @printlist = Printlist.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def printlist_params 
     params.require(:printlist).permit(:product_id, :poster_id, :cartaze_id) 
    end 

    def calcular_preco(na0, na1, na2, na3, na4, na5, na6) 
    #formulando preco 
    #precos = {a0: 1, a1: 2, a2: 3, a3: 4, a4: 5, a5: 6, a6: 7} 
    @cartaz = Cartaze.all 
    pr = HashWithIndifferentAccess.new(@cartaz.map{ |c| [c.size.to_s,c.price.to_f] }.to_h) 
    #precos = HashWithIndifferentAccess.new({a0: 1, a1: 2, a2: 3, a3: 4, a4: 5, a5: 6, a6: 7}) 
    @preco_total = (na0 * pr[:A0]) + (na1 * pr[:A1]) + (na2 * pr[:A2]) + (na3 * pr[:A3]) + (na4 * pr[:A4]) + (na5 * pr[:A5]) + (na6 * pr[:A6]) 
    end 
    #def product_params 
    #params.require(:product_id).permit(:product_id) 
    #end 
    #def poster_params 
    # params.require(:poster).permit(:id) 
    #end 
    #def cartaze_params 
    # params.require(:cartaze).permit(:id, :size) 
    #end 
end 

new.html.erb.rb Form:

<%= render "/partials/sidebar" %> 
<div class="conteudo-dash"> 
    <div class="linha-dash"> 
     <div class="panel panel-defaul col-md-6"> 
      <div class="panel-body"> 
       <h2>Cadastro de Cartaz</h2> 
       <%= form_for(@printlist, url: printlists_path) do |f|%> 

       <%= f.collection_select(:product_id, Product.all, :id, :title,{}, {class: "form-control col-md-3"}) %> 
       <%= f.collection_select(:poster_id, Poster.all, :id, :name,{},{class: "form-control"}) %> 
       <%= f.collection_select(:cartaze_id, Cartaze.all, :id, :size, {},{class: "form-control"}) %><br> 
       <%= f.submit "Salvar Produto", class: "btn btn-success" %> 
       <% end %> 
      </div> 
     </div> 
    </div> 
</div> 
</div> 

printlist.rb Modell:

class Printlist < ApplicationRecord 
    include ActiveModel::ForbiddenAttributesProtection 

    belongs_to :posters 
    belongs_to :cartazes 
    belongs_to :products 
end 

schema.rb:

ActiveRecord::Schema.define(version: 20170523202905) do 

    # These are extensions that must be enabled in order to support this database 
    enable_extension "plpgsql" 

    create_table "cartazes", force: :cascade do |t| 
    t.string "product" 
    t.decimal "price",  precision: 7, scale: 2 
    t.string "tipo" 
    t.string "size" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    end 

    create_table "dashboards", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "products_id" 
    t.integer "posters_id" 
    t.integer "cartazes_id" 
    t.index ["cartazes_id"], name: "index_dashboards_on_cartazes_id", using: :btree 
    t.index ["posters_id"], name: "index_dashboards_on_posters_id", using: :btree 
    t.index ["products_id"], name: "index_dashboards_on_products_id", using: :btree 
    end 

    create_table "dashboards_posters", id: false, force: :cascade do |t| 
    t.integer "dashboard_id", null: false 
    t.integer "poster_id", null: false 
    t.index ["dashboard_id", "poster_id"], name: "index_dashboards_posters_on_dashboard_id_and_poster_id", using: :btree 
    end 

    create_table "posters", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "printlists", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "product_id" 
    t.integer "poster_id" 
    t.integer "cartaze_id" 
    t.index ["cartaze_id"], name: "index_printlists_on_cartaze_id", using: :btree 
    t.index ["poster_id"], name: "index_printlists_on_poster_id", using: :btree 
    t.index ["product_id"], name: "index_printlists_on_product_id", using: :btree 
    end 

    create_table "products", force: :cascade do |t| 
    t.string "title" 
    t.decimal "price" 
    t.string "tipo" 
    t.string "size" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.decimal "preco_promo", precision: 7, scale: 2 
    t.string "codref" 
    end 

    create_table "servicos", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "users", force: :cascade do |t| 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.inet  "current_sign_in_ip" 
    t.inet  "last_sign_in_ip" 
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree 
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 
    end 

    add_foreign_key "dashboards", "cartazes", column: "cartazes_id" 
    add_foreign_key "dashboards", "posters", column: "posters_id" 
    add_foreign_key "dashboards", "products", column: "products_id" 
end 

EDIT

routes.rb

Rails.application.routes.draw do 
    get 'notification/create' 

    resources :products 
    devise_for :users 
    root 'home#index' 
# user_root_path 'dashboard#index' # DEPOIS DO CLIENTE ESTAR LOGADO redireciona pra dashboard 
post 'pgtcheckout/create' 
post 'notification', to: 'notification#create' 
resources :users 
get 'pgtcheckout' => 'pgtcheckout#index' 
get 'pgtcheckout/new' => 'pgtcheckout#new' 
resources :dashboards 
resources :product 
resources :posters 
resources :printlists 

Konsolenausgabe

Started POST "/printlists" for ::1 at 2017-05-29 13:53:04 -0300 
Processing by PrintlistsController#create as HTML 
    Parameters: {"utf8"=>"V", "authenticity_token"=>"KrKq5k71/exf2nnw56F43IbbZz7CRdjnsvTSulSNAM9Uj8lPB8tnQ9BfDifeXlMLVTNqIs6/T1dSeIS5zeph 
sQ==", "printlist"=>{"product_id"=>"7", "poster_id"=>"4", "cartaze_id"=>"3"}, "commit"=>"Salvar Produto"} 
    User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1 
]] 
    (0.0ms) BEGIN 
    (0.0ms) ROLLBACK 
    Rendering printlists/new.html.erb within layouts/application 
    Rendered partials/_sidebar.html.erb (12.0ms) 
    Product Load (1.0ms) SELECT "products".* FROM "products" 
    Poster Load (0.0ms) SELECT "posters".* FROM "posters" 
    Cartaze Load (0.0ms) SELECT "cartazes".* FROM "cartazes" 
    Rendered printlists/new.html.erb within layouts/application (52.0ms) 
Completed 200 OK in 1117ms (Views: 1106.2ms | ActiveRecord: 2.0ms) 
Output

Debug:

--- !ruby/object:ActionController::Parameters 
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
    utf8: "✓" 
    authenticity_token: KrKq5k71/exf2nnw56F43IbbZz7CRdjnsvTSulSNAM9Uj8lPB8tnQ9BfDifeXlMLVTNqIs6/T1dSeIS5zephsQ== 
    printlist: !ruby/object:ActionController::Parameters 
    parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
     product_id: '1' 
     poster_id: '3' 
     cartaze_id: '3' 
    permitted: false 
    commit: Salvar Produto 
+1

Bitte die vollständige Fehlermeldung posten. – Gerry

+0

Eigentlich habe ich keine Fehler, wenn ich es einreiche leitet es mich nur auf die Indexroute um, aber es bleibt auf "neue Seite". – despin

Antwort

0

Das neue Objekt Printlist eine Validierung fehlschlägt, wenn @printlist.save auf create Aktion ausführt; Deshalb erhalten Sie eine (0.0ms) ROLLBACK in Ihren Protokollen.

So gibt @printlist.save false zurück und führt format.html { render :new } aus; wie in create Aktion instruiert:

if @printlist.save 
    format.html { redirect_to printlists_path , notice: 'Impression was successfully created.' } 
    format.json { render :show, status: :created, location: @printlist } 
else 
    format.html{ render :new } 
    format.json{ render json: @printlistst.errors, status: :unprocessable_entity } 
end 

die Fehler zu erkennen, verwendet save! instad von save (dies nur vorübergehend ist), wird diese eine Ausnahme auslösen beschreibt, warum das Objekt nicht gültig ist, und spart damit nicht.

Mit save! an Ort und Stelle, jetzt erhalten Sie eine Ausnahme (anstelle eines ROLLBACK) in Ihren Protokollen mit der Fehlermeldung.

Sobald Sie feststellen, warum Ihre Validierung fehlschlägt, können Sie es beheben; und vergessen Sie nicht, save! zu save zurück zu ändern.

+0

Ich hatte einen Formularfehler auf der Seite index.html und dies half, den Fehler zu zeigen – despin

Verwandte Themen