看来我的提交按钮不起作用。可能是什么问题呢?

It seems my submit button doesn't work. What could be the problem?

没有错误提示,可以帮助定位和修复问题。我检查了数据库文件,它仍然是空的。

提交按钮<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:

解决方案是您应该在参数中提供 engine 和 user_id,或者您应该删除 presence true 验证并从模型中添加可选的 true case(用于用户关联)。

解释:

如果您的模型说它应该验证 engine 的存在,那么您怎么能不提供引擎参数(以形式)。当您 post 没有引擎的形式时,会发生什么情况是您的模型没有保存它,并且在您处理了这种情况后,它继续前进。由于它属于用户,因此用户 ID 也是如此。尽管您也可以通过在模式和模型中添加 optional: true 使其成为可选的(因为汽车可以 "be" 没有用户 IRL 但取决于您的用例)。

更进一步,要自己准确了解问题,请使用 pry 或 byebug 查看运行时的参数和事件。验证错误的更简单快捷的方法是在创建方法前添加一个爆炸,它会显示如下错误:if @car.save!。还有一件事:复制汽车参数并尝试在 rails 中用 bang 手动安慰自己。它会给你原因。这些东西将帮助您诊断 rails.

中的模型 save/create 问题

快乐编码:)

来自您的日志文件

Started POST "/cars" for ::1 at 2020-01-26 14:45:06 +0000

表明正在汽车控制器上调用创建操作 传入的参数是

{"authenticity_token"=>"Oom+xdVDc0PqSwLbLIEP0R8H6U38+v9ISVql4Fr/0WSxZGSrxzTHccsgghd1U30OugcUBAA1R4BtsB0YigAUtA==", "car"=>{"vehicle_type"=>"Sports", "car_type"=>"Private", "seat"=>"5", "colour_type"=>"Black", "transmission_type"=>"Automatic"}, "commit"=>"Create car"}

创建操作中调用的第一个方法是 authenticate_user

before_action :authenticate_user!, except: [:show]

你可以看到发生了这种情况

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

这意味着 else 子句被命中 render :new, notice: "Something went wrong..."

@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

因此汽车没有保存,所以验证一定失败了。 您的新汽车表格应该有一个错误循环来显示所有错误。如果它确实有这个那么你的用户(你)就会知道出了什么问题

像这样

  <% if car.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(car.errors.count, "error") %> prohibited this car from being saved:</h2>

      <ul>
        <% car.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

应该可以解决问题,但它在所有 rails 生成的表单中都是标准的,所以无论如何它都应该存在