web-dev-qa-db-ja.com

Rails-考案-ログイン時のエラーメッセージ?

f.error_messagesをここで機能させるにはどうすればよいですか、またはフラッシュを使用する必要がありますか?
その場合、sessions_controllerで何をオーバーライドする必要がありますか?

<h2>Create an account</h2>    
<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email, :class => :big %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password, :class => :big %>
  </p>
  <p>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, :class => :big %>
  </p>

  <p><%= f.submit "Create", :class => :submit %></p>
<% end %>

PS。 f.error_messagesアカウントの作成は完全に機能します。

36
Frexuz

確かに少しハッキーですが、私はこのヘルパー(app/helpers/devise_helper.rb)を使用してフラッシュを取得し、設定されている場合はそれらを使用して、デフォルトでresource.errors

module DeviseHelper

  def devise_error_messages!
    flash_alerts = []
    error_key = 'errors.messages.not_saved'

    if !flash.empty?
      flash_alerts.Push(flash[:error]) if flash[:error]
      flash_alerts.Push(flash[:alert]) if flash[:alert]
      flash_alerts.Push(flash[:notice]) if flash[:notice]
      error_key = 'devise.failure.invalid'
    end

    return "" if resource.errors.empty? && flash_alerts.empty?
    errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages

    messages = errors.map { |msg| content_tag(:li, msg) }.join
    sentence = I18n.t(error_key, :count    => errors.count,
                                 :resource => resource.class.model_name.human.downcase)

    html = <<-HTML
    <div id="error_explanation">
      <h2>#{sentence}</h2>
      <ul>#{messages}</ul>
    </div>
    HTML

    html.html_safe
  end

end
32
typeoneerror

これらをあなたのレイアウトに入れてみてください:

<%= content_tag(:div, flash[:error], :id => "flash_error") if flash[:error] %>
<%= content_tag(:div, flash[:notice], :id => "flash_notice") if flash[:notice] %>
<%= content_tag(:div, flash[:alert], :id => "flash_alert") if flash[:alert] %>

Deviseのログインアクションは、モデルエラーではなくフラッシュメッセージを設定します。

65
ffoeg

この投稿の年齢にもかかわらず、私はDeviseを使い始めたときに問題を抱えていた私のような人々を助けるための解決策を共有したいと思いました。物事を維持するためにDRY私はこのコードをapplication.html.erbファイル:

<body>
  <% flash.each do |key, value| %>
    <div class="flash <%= key %>"><%= value %></div>
  <% end %>

  <%= yield %>
</body>
5
Carl Edwards

これもしなければならない

<% flash.each do |name, msg| %>
  <%= content_tag :div, msg, id: "flash_#{name}" %>
<% end %>
3
Sachin

ヘルパーを作成する

 # app/helpers/application_helper.rb

    module ApplicationHelper

      def flash_class(level)
        case level
          when 'info' then "alert alert-info"
          when 'notice','success' then "alert alert-success"
          when 'error' then "alert alert-danger"
          when 'alert' then "alert alert-warning"
        end
      end

    end

#view
<% flash.each do |key, value| %>
   <div class="<%= flash_class(key) %> fade in">
      <a href="#" class="close" data-dismiss="alert">&times;</a>
      <%= value %>
   </div>
<% end %>
0
gilcierweb