在rails 5中显示表单验证错误

时间:2017-01-24 06:43:42

标签: ruby-on-rails ruby forms error-handling ruby-on-rails-5

我在使用嵌套的rails表单在视图中显示验证错误时遇到了一些困难

控制器:

class RentersController < ApplicationController
  before_action :set_renter, only: [:show, :edit, :update, :destroy]
  before_action :get_rental

  def get_rental
    @rental = Rental.find(params[:rental_id])
  end

    ...

  # GET /renters/new
  def new
    @renter = Renter.new
  end

  ...

  def create
    @renter = @rental.renters.new(renter_params)
    respond_to do |format|
      if @renter.save
        format.html { redirect_to rental_renters_path(@rental), notice: 'Renters were successfully created.' }
        format.json { render :show, status: :created, location: @renter }
      else
        puts @renter.errors.full_messages
        format.html { render :new }
        format.json { render json: @renter.errors, status: :unprocessable_entity }
      end
    end
  end

  ...

end

模型

class Renter < ApplicationRecord
  belongs_to :rental

  validates :name,      presence:  { message: "..." }
  validates :height,    presence:  { message: "..." }
  validates :weight,    presence:  { message: "..." }
  validates :shoeSize,  presence:  { message: "..." }

end

_形成部分在视图中呈现

<div class="rental-forms-container sixteen wide column">
  <%= form_for([@rental, @renter], remote: true, :html => { class: "renter-form ui form", id: "base-form" }) do |f| %>

    <div class="fields">

      ...

    </div>

  <% end %>
</div>

<div class="ui warning message">
  ...
  <ul class="list">
    <% @renter.errors.messages.values.each do |message| %>
      <%  message.each do |m| %>
        <li><%= m %></li>
      <% end %>
    <% end %>
  </ul>
</div>

...

    <%= link_to 'continue with booking', rental_renters_path, remote: true, class: 'ui teal submit button', id: 'submitRenterForms' %>

</div>

控制台

Processing by RentersController#create as JS
Processing by RentersController#index as JS
  Parameters: {"utf8"=>"✓", "renter"=>{"name"=>"", "height"=>"", "weight"=>"", "wetsuit_style"=>"Adult Womens", "shoeSize"=>"", "rental_id"=>""}, "rental_id"=>"109"}
  Parameters: {"rental_id"=>"109"}
  Rental Load (0.3ms)  SELECT  "rentals".* FROM "rentals" WHERE "rentals"."id" = $1 LIMIT $2  [["id", 109], ["LIMIT", 1]]
  Rental Load (5.5ms)  SELECT  "rentals".* FROM "rentals" WHERE "rentals"."id" = $1 LIMIT $2  [["id", 109], ["LIMIT", 1]]
   (0.2ms)  BEGIN
   (0.1ms)  ROLLBACK
Name Let us know the name of each renter so we can customize your experience
Height Let us know the height of each renter so we can properly size your wetsuits
Weight Let us know the weight of each renter so we can properly size your wetsuits
Shoesize Let us know the shoe size of each renter so everyone gets the right surf booties
  Rendering renters/new.html.erb within layouts/application
  Rendered renters/_form.html.erb (2.4ms)
  Rendered renters/new.html.erb within layouts/application (3.7ms)
  Rendered shared/_following_menu.html.erb (0.1ms)
  Rendered shared/_sidebar_menu.html.erb (0.1ms)
  Rendered shared/_menu.html.erb (0.8ms)
  Rendered shared/_footer.html.erb (0.5ms)
Completed 200 OK in 106ms (Views: 73.8ms | ActiveRecord: 14.0ms)


   (0.6ms)  SELECT COUNT(*) FROM "renters" WHERE "renters"."rental_id" = $1  [["rental_id", 109]]
  Rendering renters/index.html.erb within layouts/application
  Charge Load (0.3ms)  SELECT  "charges".* FROM "charges" WHERE "charges"."rental_id" = $1 LIMIT $2  [["rental_id", 109], ["LIMIT", 1]]
  Rendered rentals/_info.html.erb (11.0ms)
  Renter Load (0.3ms)  SELECT "renters".* FROM "renters" WHERE "renters"."rental_id" = $1  [["rental_id", 109]]
  Rendered charges/_form.html.erb (1.9ms)
  Rendered renters/index.html.erb within layouts/application (37.5ms)
  Rendered shared/_following_menu.html.erb (0.6ms)
  Rendered shared/_sidebar_menu.html.erb (0.5ms)
  Rendered shared/_menu.html.erb (1.2ms)
  Rendered shared/_footer.html.erb (1.4ms)
Completed 200 OK in 265ms (Views: 134.5ms | ActiveRecord: 9.5ms)

验证错误正在输出到终端,但它们没有出现在视图中。

我尝试过使用flash和会话哈希将它们传递给视图,但无济于事。任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用form_for时(或者如果您现在使用form_with)请注意remote或local是否设置为true。 Local使用浏览器的常规提交机制,而远程使用Ajax。使用遥控器时(如上所示),页面将不会按照预期的“正常”方式呈现。

Rails文档包含更多详细信息:http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html