Alex Nikolsky
Alex Nikolsky

Reputation: 2179

Flash errors displaying issue

Why aren't flash errors and notices rendering in project?

While I'm using this non-flash form for my signup

views/users/new.html

<h1>Sign Up</h1>

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% @user.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

 <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </div>
 <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions"><%= f.submit %></div>
<% end %>

It works good, however It makes some indents between fields for some mysterious reasons.

enter image description here

Meanwhile flash errors don't want to show up on login form.

views/sessions/new.html

<h1>Log In</h1>

<%= form_tag '/authenticate' do %>
  <div class="field">
    <%= label_tag :email %><br />
    <%= text_field_tag :email, params[:email] %>
  </div>
  <div class="field">
    <%= label_tag :password %><br />
    <%= password_field_tag :password %>
  </div>
  <div class="actions"><%= submit_tag "Log In" %></div>
<% end %>

I can't see notices and alerts as well. It simply doesn't show up.

controllers/sessions_controller.rb

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
      session[:user_id] = user.id
      redirect_to users_path, notice: "Logged In!"
    else
      flash.now.alert = "Email or password is invalid"
      render "new"
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to signup_path, notice: "Logged out!"
  end

end

controllers/users_controller.rb

class UsersController < ApplicationController
before_filter :authorize, only: [:edit, :destroy]

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      session[:user_id] = @user.id
      redirect_to [@user, @task], notice: "Thank you for signing up!"
    else
      render "new"
    end
  end

  def index
    @users = User.all
  end

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

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

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

    if @user.update(user_params)
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    respond_to {|format| format.js }
  end

  private


  def user_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
  end
end

What do I do?

Upvotes: 1

Views: 338

Answers (2)

Tim
Tim

Reputation: 1376

1

Below is a variation of Athar's answer to my personal liking. I feel it is more succinct and readable. As Athar mentioned prior, you should add it to your application.html.erb file inside body.

<% flash.each do |message_type, message| %>
   <div class="flash-<%= message_type %>"><%= message %></div>
<% end %>

2

Inside of your sessions_controller.rb and users_controller.rb files respectively you have the following lines of code...

  • redirect_to users_path, notice: "Logged In!"
  • redirect_to signup_path, notice: "Logged out!"
  • redirect_to [@user, @task], notice: "Thank you for signing up!"

...however, a possible problem is that you are never actually calling the flash helper method.

Hence, I would put all your notices inside a flash helper, like so...

flash: { notice: "This notice is inside the flash helper!" }


3

Also, try using flash.now[:alert], instead of flash.now.alert.


Hopefully this might solve your problem.

Upvotes: 1

Athar
Athar

Reputation: 3268

add this to your application.html.erb

<% if flash[:error].present? %>
  <p class='flash-error'><%= flash[:error] %></p>
<% end %>
<% if flash[:notice]? %>
  <p class='flash-notice'><%= flash[:notice] %></p>
<% end %>

or you can also follow this link http://pupeno.com/2009/11/19/ensuring-the-displaying-of-flash-messages-in-ruby-on-rails/

Upvotes: 1

Related Questions