我正在尝试在Printlist和Product,Template和Paper Type之间建立关联,但是我收到了一个错误。当我提交新表单时,它会重定向到索引路由,但会在同一个新页面上保留,并且不会将信息保存到数据库。
控制器:
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表单:
<%= 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型号:
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
修改
的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
控制台输出
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)
调试输出:
--- !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
答案 0 :(得分:0)
在Printlist
操作上执行@printlist.save
时,新的create
对象未通过验证;这就是您在日志中获得(0.0ms) ROLLBACK
的原因。
因此,@printlist.save
返回false并执行format.html { render :new }
;按照create
行动中的指示:
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
要发现错误,请使用save!
save
的instad(这只是暂时的),这将引发一个异常,说明对象无效的原因,从而无法保存。
在save!
到位的情况下,现在您将在日志中收到异常(而不是ROLLBACK),并显示错误消息。
一旦发现验证失败的原因,就可以修复它;并且不要忘记将save!
更改回save
。