I filled in all the files as indicated in the listings, but I don't get an error field when I enter invalid data. I have restarted rails server several times, but nothing has changed. Tests with invalid data pass as needed.
my "app/views/users/new.html.erb" file:
<% title "Sign up" %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
my "app/controllers/users_controller.rb" file:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
#
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
my "app/models/user.rb" file:
class User < ApplicationRecord
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEXP = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEXP },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
and my "app/views/shared/_error_messages.html.erb" file:
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
tap and when I click "Create my account" this just refresh page
upd: I carefully checked my code with the code from the repository of Michael Hartl, but I didn't find anything strange, maybe something has changed in the gems that I use.
In one repository I found a solution for rails version 7, in rails 5 everything worked with the code I provided above, but in version 7, in order for the error field to be displayed, you need to add an HTTP status code.
Adding status: :unprocessable_entity
after render 'new'
in the controller file solved my problem.