I'm new to rails...but I'm trying to do an app on my own to "practice" what I've learned.
I have a new form with model validations, but the error messages aren't showing. Here is what I have:
seed.rb
(Model)
class Seed < ApplicationRecord
validates :name, presence: true
validates :category, presence: true
validates :latin, presence: true
validates :maturity, presence: true
validates :sun, presence: true
validates :sow, presence: true
validates :cycle, presence: true
validates :description, presence: true, length: { minimum: 5, maximum: 500 }
mount_uploader :seedimage, SeedImageUploader
end
seed_controller.rb
(Controller)
class SeedsController < ApplicationController
def index
@seeds = Seed.all
end
def new
@seed = Seed.new
end
def create
@seed = Seed.new(seed_params)
if @seed.save
redirect_to seeds_path
else
render 'new'
end
end
def edit
end
def update
@seed = Seed.find(params[:id])
if @seed.update(seed_params)
redirect_to @seed
else
render 'edit'
end
end
def show
@seed = Seed.find(params[:id])
end
def destroy
@seed = Seed.find(params[:id])
@seed.destroy
redirect_to seeds_path
end
def seed_params
params.require(:seed).permit(:name, :category, :latin, :maturity, :sun, :sow, :cycle, :description, :seedimage)
end
end
_form.html.erb
(Form for 'New') ( new.html.erb
just has <% render 'form' %>
<%= form_with model: @seed, class: "form-horizontal" do |f| %>
<% if @seed.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@seed.errors.count, "error") %> prohibited
this seed from being saved:
</h2>
<ul>
<% @seed.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: "Seed Name" %>
</div>
<div class="form-group">
<%= f.label :category %>
<%= f.text_field :category, class: "form-control", placeholder: "Category: 'beans'" %>
</div>
<div class="control-label">
<%= f.label :latin %>
<%= f.text_field :latin, class: "form-control", placeholder: "Latin Name" %>
</div>
<div class="form-group">
<%= f.label :maturity %>
<%= f.number_field :maturity, class: "form-control", placeholder: "Maturity Time" %>
</div>
<div class="form-group">
<%= f.label :sun %>
<%= f.select(:sun, options_for_select([['Full Sun'], ['Partial Sun'], ['Full Shade']]), {}, { class: "custom-select"}) %>
</div>
<div class="form-group">
<%= f.label :sow %>
<%= f.text_field :sow, class: "form-control", placeholder: "Plant Indoors/Sow Outdoors/etc.." %>
</div>
<div class="form-group">
<%= f.label :cycle %>
<%= f.text_field :cycle, class: "form-control", placeholder: "Annual/Perennial/etc.." %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, size: "60x12", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :seedimage %>
<%= f.file_field :seedimage, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Create", class: "btn btn-primary btn-lg" %>
</div>
<% end %>
I'm a bit confused why this doesn't work? Right now when I hit the create button it flashes, but no error messages. I can CONFIRM that the model is using the validations because if I try to do a Seed.create()
and check messages against that it doesn't indeed work....so I'm a bit confused?
From what I can tell the .any?
isn't happening, since if I do a !
to that statement it'll at least display 0 messages
.
You are not seeing any error messages because all the forms generated by form_with
tag are remote: true
by default and send xhr(ajax) requests to the server. If you want to see the error messages, as of your current setup, you have to add local: true
wich makes the submission normal.
Replacing
<%= form_with model: @seed, class: "form-horizontal" do |f| %>
with
<%= form_with model: @seed, local: true, class: "form-horizontal" do |f| %>
will do the trick. Hope this will help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.