如何在控制器中添加类

时间:2015-12-10 08:14:36

标签: ruby-on-rails ruby ruby-on-rails-3 model-view-controller

我想在控制器中添加一个类来改变if函数

中出现的消息的颜色

这是我的控制器

def send_password
    @user=User.find_by_email(params[:user][:email])
    if @user.present?
      @user.send_reset_password_instructions
      flash[:notice] = "Reset password instructions have been sent to #{@user.email}."
      redirect_to reset_password_path, notice: "Reset password instructions have been sent to #{@user.email}."
    else
      return redirect_to reset_password_path, notice: "User is not available."
    end
  end
end

请帮忙。

我尝试并以这样的形式给出了,但它没有用。

<%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: {method: :post}) do |f| %>
   <% if f.error_notification == 'User is not available' %>
       <h1 class='reset-error-message'><%= f.error_notification %></h1>
   <% else %>
       <h1 class='reset-success-message'><%= f.error_notification %></h1>
   <% end %>`

然后在控制器中我尝试了这样也无效

if @user.present?
  @user.send_reset_password_instructions
  flash[:notice] = "Reset password instructions have been sent to #{@user.email}."
  redirect_to reset_password_path, notice: "Reset password instructions have been sent to #{@user.email}.",class:"reset-success-message"
else
  return redirect_to reset_password_path, notice: "User is not available., class:"reset-error-message"
end

1 个答案:

答案 0 :(得分:0)

最好为不同的通知使用不同的Flash消息类:

# Use flash *notice*
redirect_to reset_password_path, notice: "Reset password instructions have been sent to #{@user.email}."

# Use flash *alert* (error)
return redirect_to reset_password_path, alert: "User is not available."

在布局或视图中,您现在可以使用自己的CSS类渲染每个Flash:

<% flash.each do |name, msg| -%>
  <%= content_tag :div, msg, class: name %>
<% end -%>