首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 5.2.3 -验证错误显示在控制台中,但未传递给视图

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

Stack Overflow用户
提问于 2019-09-07 03:19:29
回答 1查看 345关注 0票数 1

当我提交一个包含所需空白字段的表单时,我会在控制台中看到ROLLBACKActiveModel::Errors,但由于某种原因,我无法在视图中呈现错误。

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

这就是我的代码的样子:

模型:transactions.rb

代码语言:javascript
复制
class Transaction < ApplicationRecord
  belongs_to :account
  belongs_to :category

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

控制器:transactions_controller.rb

代码语言:javascript
复制
 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) )

代码语言:javascript
复制
  <ul>
    <% @transaction.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
  </ul>

提交表单后的日志

代码语言:javascript
复制
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,则会得到以下错误:

代码语言:javascript
复制
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}]}>

我尝试的另一种方法是将这2行添加到new.html.erb

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-07 04:38:39

Processing by TransactionsController#create as JS,您的表单是使用ajax提交的,您需要呈现js视图或使用非ajax请求。您的表单是否使用form_withform_with默认为远程窗体,使用form_for代替,或者使用js响应更新视图。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57830404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档