【问题标题】:It seems my submit button doesn't work. What could be the problem?看来我的提交按钮不起作用。可能是什么问题呢?
【发布时间】: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


【解决方案1】:

TLDR:

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

说明:

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

更进一步,要自己准确理解问题,请使用 pry 或 byebug 来查看运行时的参数和事件。验证错误更简单快捷的方法是在 create 方法前面加上一个 bang,它会显示如下错误:if @car.save!。还有一件事:复制汽车参数并尝试在rails控制台中手动执行此操作。它会给你原因。这些东西将帮助您诊断模型保存/创建 Rails 中的问题。

快乐编码:)

【讨论】:

  • 好的,谢谢。所以我没有在我的表格中包含引擎。我现在已经做到了,并且有效。谢谢。
  • @Richard 那么我想你应该接受答案,因为它准确地解决了你的问题:-)
  • 是的,谢谢,但是我什至不能投票,因为我的声望不到 15。
  • @Richard 问题投票选项下方有一个勾号。那是接受选项,接受答案后,您将有足够的声誉来支持我的答案。来吧,你是开发者。
  • @Richard 谢谢。作为一个新的积极的贡献者/帮助者,如果有人接受/投票赞成锥子的正确答案,这对激励有很大帮助。
【解决方案2】:

来自您的日志文件

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 生成的表单中都是标准的,所以无论如何它都应该存在

【讨论】:

    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 2016-12-02
    • 2011-12-14
    • 2021-08-25
    • 2017-07-28
    • 2016-04-12
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多