【发布时间】:2020-05-12 01:32:36
【问题描述】:
它没有显示任何错误以帮助定位和解决问题。我检查了数据库文件,它仍然是空的。
提交按钮<div><%= f.submit "Create", class: "btn btn-normal" %></div>
提交后唯一改变的是地址。它从http://localhost:3000/cars/new 变为http://localhost:3000/cars
其他一切都保持不变。我该如何解决这个问题?
用以下内容更新了问题;
日志
Started GET "/cars/new" for ::1 at 2020-01-26 14:44:53 +0000
(0.1ms) SELECT sqlite_version(*)
Processing by CarsController#new as HTML
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering cars/new.html.erb within layouts/application
Rendered cars/new.html.erb within layouts/application (Duration: 12.1ms | Allocations: 1210)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered shared/_navbar.html.erb (Duration: 0.7ms | Allocations: 103)
Rendered shared/_message.html.erb (Duration: 0.1ms | Allocations: 17)
Completed 200 OK in 496ms (Views: 471.7ms | ActiveRecord: 1.0ms | Allocations: 15750)
Started POST "/cars" for ::1 at 2020-01-26 14:45:06 +0000
Processing by CarsController#create as HTML
Parameters: {"authenticity_token"=>"Oom+xdVDc0PqSwLbLIEP0R8H6U38+v9ISVql4Fr/0WSxZGSrxzTHccsgghd1U30OugcUBAA1R4BtsB0YigAUtA==", "car"=>{"vehicle_type"=>"Sports", "car_type"=>"Private", "seat"=>"5", "colour_type"=>"Black", "transmission_type"=>"Automatic"}, "commit"=>"Create car"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering cars/new.html.erb within layouts/application
Rendered cars/new.html.erb within layouts/application (Duration: 7.2ms | Allocations: 1144)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered shared/_navbar.html.erb (Duration: 0.2ms | Allocations: 103)
Rendered shared/_message.html.erb (Duration: 0.1ms | Allocations: 17)
Completed 200 OK in 124ms (Views: 114.9ms | ActiveRecord: 0.4ms | Allocations: 14757)
型号 app/models/car.rb
class Car < ApplicationRecord
belongs_to :user
validates :vehicle_type, presence: true
validates :car_type, presence: true
validates :seat, presence: true
validates :transmission_type, presence: true
validates :engine, presence: true
end
控制器 app/controllers/cars_controller.rb
class CarsController < ApplicationController
before_action :set_car, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]
def index
@cars = current_user.cars
end
def new
@car = current_user.cars.build
end
def create
@car = current_user.cars.build(car_params)
if @car.save
redirect_to listing_car_path(@car), notice: "Saved..."
else
render :new, notice: "Something went wrong..."
end
end
def show
end
def listing
end
def pricing
end
def description
end
def photo_upload
end
def features
end
def location
end
def update
if @car.update(car_params)
flash[:notice] = "Saved..."
else
flash[:notice] = "Something went wrong..."
end
redirect_back(fallback_location: request.referer)
end
private
def set_car
@car = Car.find(params[:id])
end
def car_params
params.require(:car).permit(:vehicle_type, :car_type, :seat, :transmission_type, :engine, :fuel_type, :colour_type, :window_type, :listing_name, :summary, :is_tv, :is_air, :is_internet, :is_sunroof, :is_bluetooth, :is_dvd, :is_gps, :is_usb, :is_audio, :is_airbags, :price, :active)
end
end
【问题讨论】:
-
你检查过你的日志吗?根据可用信息,我最好的猜测是您的模型未通过验证,并且您没有测试它是否正确保存在您的
create操作中。 -
请在您的视图中显示表单定义和负责的控制器操作。此外,请在表单提交之后包含显示对服务器的请求的日志条目。
-
你能发布这个资源的控制器代码吗,如果你能发布一份发送过来的参数副本也会有所帮助。鉴于路由发生变化,听起来某种验证不起作用,或者需要将参数列入白名单。
-
从您的日志看来,您有验证错误,因此重新呈现新的。您可能希望在表单上显示这些验证错误以提供一些用户反馈,但如果您只是想深入了解它然后调试(例如,在保存之前执行
puts @car.errors.messages。从您的参数来看,您似乎缺少:engine和:user_id,您的模型需要。 -
我建议你看看这个文档:guides.rubyonrails.org/…
标签: ruby-on-rails ruby windows-subsystem-for-linux