ruby-on-rails - 看来我的提交按钮不起作用。可能是什么问题呢?
问题描述
它没有显示任何错误以帮助定位和解决问题。我检查了数据库文件,它仍然是空的。
提交按钮<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
解决方案
TLDR:
解决方案是您应该在参数中提供引擎和 user_id,或者您应该删除存在真实验证并从模型中添加可选的真实案例(用于用户关联)。
解释:
如果您的模型说它应该验证存在,engine
那么您怎么能不提供引擎参数(形式)。当您在没有引擎的情况下发布表单时,发生的情况是您的模型没有保存它,并且当您处理了这种情况时,它会继续前进。由于它属于用户,因此用户 ID 也是如此。尽管您也可以通过在模式和模型中添加 optional: true 来使其成为可选(因为汽车可以在没有用户 IRL 的情况下“成为”,但取决于您的用例)。
更进一步,要自己准确理解问题,请使用 pry 或 byebug 来查看运行时的参数和事件。验证错误的更简单快捷的方法是在 create 方法前面加上一个 bang,它会显示如下错误:if @car.save!
. 还有一件事:复制汽车参数并尝试在rails控制台中手动执行此操作。它会给你原因。这些东西将帮助您诊断 Rails 中的模型保存/创建问题。
快乐编码:)
推荐阅读
- python - Python statistics.median_grouped 返回输入范围之外的值
- c++ - 关于阅读有效c++项目4的问题(用局部静态变量替换非局部静态变量)
- python - 使用用户输入的关键字在 CSV 文件中搜索项目
- java - 从终端向 Java swing 应用程序发送命令
- c++ - structname &function 在以下示例中如何工作?
- delphi - 有没有办法模仿信用卡机器的屏幕?
- algorithm - 用最小查询找到升序系列的结尾
- java - 从 ArrayList 中获取字符串。(带数字)
- python - 获取当前操作系统语言 - Python
- c++ - 在 C++ 中从 X 毫秒前获取时间戳