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

问题描述 投票:0回答:1

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

[提交按钮<div><%= f.submit "Create", class: "btn btn-normal" %></div>

提交后唯一更改的是地址。从http://localhost:3000/cars/new变为http://localhost:3000/cars

其他所有内容保持不变。我该如何解决?

用以下内容更新了问题;

Log

    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

Controller 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
ruby-on-rails ruby windows-subsystem-for-linux
1个回答
0
投票

[如果您的模型说它应该验证engine的存在,那么您如何不提供引擎参数(以表格形式)。当您发布不带引擎的表单时,发生的事情是您的模型没有保存它,并且在您处理完这种情况后,它就继续前进。

解决方案是您应该在参数中提供引擎,或者应该从模型中删除真实状态验证。

进一步走,要自己真正地了解问题,请使用撬或byebug查看运行时的参数和事件。验证错误的简便方法是,在create方法前加一个“ bang”,它将显示错误,例如:if @car.save!。还有一件事:复制汽车参数,并尝试在轨道控制台中手动操作。它会给你原因。这些东西将帮助您诊断模型在rails中的保存/创建问题。

快乐编码:)

© www.soinside.com 2019 - 2024. All rights reserved.