Reputation: 13
I am having some difficulty getting my nested rails form to display validation errors in the view
Controller:
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
Model
class Renter < ApplicationRecord
belongs_to :rental
validates :name, presence: { message: "..." }
validates :height, presence: { message: "..." }
validates :weight, presence: { message: "..." }
validates :shoeSize, presence: { message: "..." }
end
_form partial being rendered in View
<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>
Console
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)
The validation errors are outputting to the terminal but they are not appearing in the view.
I have tried using the flash and session hashes to pass them to the view but to no avail. Any guidance would be greatly appreciated.
Upvotes: 1
Views: 3751
Reputation: 11
When using form_for (or if you're now using form_with) pay attention to whether remote or local is set to true. Local uses the browser's normal submission mechanism, while remote uses Ajax. When using remote (as indicated above), the page will not render in the "normal" fashion, as expected.
The Rails documentation has more details: http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html
Upvotes: 1