Flash错误显示问题

时间:2015-07-19 21:11:49

标签: ruby-on-rails ruby-on-rails-4

为什么项目中没有flash错误和通知呈现?

我正在使用这种非Flash格式进行注册

视图/用户/ 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 %>

它运作良好,但由于一些神秘的原因,它在字段之间进行了一些缩进。

enter image description here

同时闪存错误不希望出现在登录表单上。

视图/会话/ 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 %>

我也看不到通知和提醒。它根本没有出现。

控制器/ 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

控制器/ 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

我该怎么办?

2 个答案:

答案 0 :(得分:1)

将此添加到您的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 %>

或者您也可以点击此链接http://pupeno.com/2009/11/19/ensuring-the-displaying-of-flash-messages-in-ruby-on-rails/

答案 1 :(得分:1)

1

以下是Athar对个人喜好的回答。我觉得它更简洁,更易读。正如Athar之前提到的,您应该将其添加到正文内的 application.html.erb 文件中。

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

2

分别在 sessions_controller.rb users_controller.rb 文件中,您有以下代码行...

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

...但是,一个可能的问题是你实际上从未实际调用flash辅助方法。

因此,我会把你的所有通知放在一个flash助手里面,就像这样......

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

3

另外,请尝试使用flash.now[:alert],而不是flash.now.alert

希望这可以解决您的问题。