msmith1114
msmith1114

Reputation: 3231

Rails errors not showing?

So im learning rails, and following the rails getting started guide (I've actually done the "blog app" on the Udemy rails course, but im making sure I can write it from scratch first before moving on).

Anyways, i've gotten Delete/Create running, but I was adding validation...and while the validation works my errors aren't showing up.

Right now my pages are super simple:

new.html.erb

<% if @user.errors.any? %>
  <div id="error_explanation">
    <h2>
      <%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved
    </h2>
    <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

<%= form_with scope: :user, url: users_path, local: true do |form| %>
<p>
  <%= form.label :username %><br>
  <%= form.text_field :username %>
</p>
<p>
  <%= form.label :name %><br>
  <%= form.text_field :name %>
</p>
<p>
  <%= form.label :age %><br>
  <%= form.number_field :age %>
</p>
<p><%= form.submit %></p>
<% end %>

users_controller

class UsersController < ApplicationController
    def index
      @users = User.all
    end

    def new
      @user = User.new
    end

    def edit
      @user = User.find(params[:id])
    end

    def create
      @user = User.new(params.require(:user).permit(:username,:name,:age))
      if @user.save
        redirect_to users_path
      else
        render 'new'
      end
    end

    def update

    end

    def destroy
      @user = User.find(params[:id])
      @user.destroy
      redirect_to users_path
    end

end

So the weird thing if I go into my network tab in dev tools I can see this show up in the response tab:

  <div id="error_explanation">
    <h2>
      1 error prohibited this user from being saved
    </h2>
    <ul>
        <li>Username has already been taken</li>
    </ul>
  </div>

But it doesn't show up in "elements" in Chrome dev tools. I've restarted rails....so Im really not sure why the elements are not showing up. I DO have bootstrap 4.00 beta installed, but not sure why that would matter. This is rails 5.1.4 btw.

Upvotes: 0

Views: 199

Answers (1)

Ritesh Ranjan
Ritesh Ranjan

Reputation: 1012

This is because you are not getting the same @user in the <% if @user.errors.any? %>

Try this :

<%= form_with scope: :user, url: users_path, local: true do |form| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved
      </h2>
      <ul>
        <% @user.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

<p>
  <%= form.label :username %><br>
  <%= form.text_field :username %>
</p>
<p>
  <%= form.label :name %><br>
  <%= form.text_field :name %>
</p>
<p>
  <%= form.label :age %><br>
  <%= form.number_field :age %>
</p>
<p><%= form.submit %></p>
<% end %>

Upvotes: 2

Related Questions