ruby - Form Error Messages Not Generating in Rails App -


hi have simple app building , having trouble getting error messages appear when inputs invalid information or no information @ field.

the form using sign new user, code associated user , form below;

users_controller.rb

class userscontroller < applicationcontroller     def index     @users = user.all   end    def show     @user = user.find(params[:id])     @country = country.all   end    def new     @user = user.new   end    def create     @user = user.new(user_params)     if @user.save       session[:user_id] = @user.id       redirect_to @user     else       redirect_to '/signup'     end   end     private       def user_params         params.require(:user).permit(:first_name, :last_name, :email, :password)     end   end 

user.rb

class user < applicationrecord    before_save { self.email = email.downcase }   validates :first_name, presence: true, length: { maximum: 25 }   validates :first_name, presence: true, length: { maximum: 50 }   valid_email_regex = /\a[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i   validates :email, presence: true, length: { maximum: 255 }, format: { with: valid_email_regex }   has_secure_password   validates :password, presence: true, length: { minimum: 6 }    has_many :trips   has_many :countries, through: :trips  end 

new.html.erb

<div class="container">   <h1 class="text-center">sign up</h1>   <div class="row">     <div class="col-md-6 offset-md-3 ">       <%=form_for(@user) |f| %>         <%= render 'shared/error_messages' %>          <%= f.label :first_name %>         <%= f.text_field :first_name, class: "form-control" %>          <%= f.label :last_name %>         <%= f.text_field :last_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.submit "create account", class: 'form-control btn btn-primary' %>       <% end %>      </div>   </div> </div> 

_error_messages.html.erb

<% if @user.errors.any? %>   <div class="alert alert-danger">     form contains <%= pluralize(@user.errors.count, "error") %>.   </div>   <ul>   <% @user.errors.full_messages.each |msg| %>     <li><%= msg %></li>   <% end %>   </ul> <% else %>   <h3>test</h3> <% end %> 

when load form see "test" string, put in _error_messages.html.erb visibility. when enter data in signup page, reloads page (as should rather sending user page when fields valid). however, "test" string still appears @ top rather error messages should.

my assumption need sort of session or remember errors were, right reloads new page nothing in memory, however, cannot find solution anywhere @ moment.

any appreciated!

as said, need change

redirect_to '/signup'  

to

render 'new' 

from guides

the render method used @user object passed new template when rendered. rendering done within same request form submission, whereas redirect_to tell browser issue request.

that said, redirect_to issues new request browser, values of @user lost, in other words @user new instance instantiated again. why <% if @user.errors.any? %> always returns false if there no errors in @user.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -