Rails 5.2.3-验证错误显示在控制台中,但不会传递给视图

时间:2019-09-07 03:19:29

标签: ruby-on-rails ruby-on-rails-5

当我提交带有必填字段的表单时,我会在控制台中看到ROLLBACKActiveModel::Errors,但是由于某种原因,我无法在视图中呈现错误。

newedit模板上都发生这种情况。 不用说,使用所需字段创建新记录或编辑现有记录都可以。

这是我的代码的样子:

型号: transactions.rb

class Transaction < ApplicationRecord
  belongs_to :account
  belongs_to :category

  validates :date, :description, :amount, :category_id, :account_id, presence: true
end

控制器: transactions_controller.rb

 def new
    @transaction = Transaction.new
  end

  def create
    @transaction = Transaction.create(transaction_params)

    if @transaction.save
      redirect_to account_path(@account), notice: "Transaction created"
    else
      render :new
    end
  end

  def edit
    if @transaction.amount > 0
      @transaction_type = "income"
    else
      @transaction_type = "expense"
    end

    render :edit
  end

  def update
    if @transaction.update(transaction_params)
      redirect_to account_path(@account)
    else
      render :edit
    end
  end

  private
    def transaction_params
      params.require(:transaction).permit(:date, :description, :amount, :category_id, :account_id)
    end

查看: new.html.erb (或 edit.html.erb

  <ul>
    <% @transaction.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
  </ul>
提交表单后,

登录

Started POST "/accounts/15/transactions" for 192.168.55.1 at 2019-09-07 03:00:24 +0000
Cannot render console from 192.168.55.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by TransactionsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"CJd+/rB6LcUqQAz0ZvY0cf8Dcu+2qzUYLCQHLWE2GElZtJrKbKq2EODFeVvKG6NkE2MxtIIjRreqHAnKu2sJ9A==", "transaction"=>{"date(1i)"=>"2019", "date(2i)"=>"9", "date(3i)"=>"7", "description"=>"", "category_id"=>"1", "amount"=>"0"}, "transaction_type"=>"Expense", "commit"=>"Add Transaction", "account_id"=>"15"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /var/lib/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  Account Load (0.1ms)  SELECT  "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2  [["id", 15], ["LIMIT", 1]]
  ↳ app/controllers/transactions_controller.rb:64
   (0.1ms)  BEGIN
  ↳ app/controllers/transactions_controller.rb:24
  Category Load (0.2ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/transactions_controller.rb:24
   (0.2ms)  ROLLBACK
  ↳ app/controllers/transactions_controller.rb:24
   (0.1ms)  BEGIN
  ↳ app/controllers/transactions_controller.rb:26
   (0.1ms)  ROLLBACK
  ↳ app/controllers/transactions_controller.rb:26
  Rendering transactions/new.html.erb within layouts/application

如果我在p @transaction.errors方法中添加create或进入Rails控制台并运行@transaction.errors,我会得到以下错误:

irb(main):026:0> @transaction.errors
=> #<ActiveModel::Errors:0x0000000006447158 @base=#<Transaction id: nil, date: nil, description: nil, amount: nil, account_id: nil, created_at: nil, updated_at: nil, category_id: 
nil>, @messages={:account=>["must exist", "can't be blank"], :category=>["must exist", "can't be blank"], :date=>["can't be blank"], :description=>["can't be blank"], :amount=>["can't be blank"]}, @details={:account=>[{:error=>:blank}, {:error=>:blank}], :category=>[{:error=>:blank}, {:error=>:blank}], :date=>[{:error=>:blank}], :description=>[{:error=>:blank}], :amount=>[{:error=>:blank}]}>

我尝试过的另一件事是将这两行添加到 new.html.erb

<%= @transaction.errors %>呈现:#<ActiveModel::Errors:0x00007ff650570500>

<%= @transaction.errors.full_messages %>呈现:[]

1 个答案:

答案 0 :(得分:1)

Processing by TransactionsController#create as JS,您的表单是使用ajax提交的,您需要呈现js视图或使用非ajax请求。您在表单中使用form_with吗? form_with默认为远程表单,请改用form_for或使用js响应更新视图。