如何使用 redirect_to 创建警报

How to create alert with redirect_to

您好,我想在创建八卦时创建成功警报,并在我的主页上 return 或在验证失败时发出危险警报

我刚刚成功设置了错误警报。

这是我的八卦控:

class GossipsController < ApplicationController
  def index
    @gossips = Gossip.all
  end

  def show
    @gossip = Gossip.find(params[:id])
  end

  def new
    @error = false
  end

  def create
    @gossip = Gossip.new(title: params[:title], content: params[:content], user: User.find(182))

    if @gossip.save
      redirect_to root_path 
    else
      @error = true
      render "new"
    end
  end
end

这是我对新事物的看法:

<% if @error %>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
      <strong>Error</strong>
      <ul>
          <% @gossip.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
        </ul>
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
<% end %>   

<h2>Create your own gossip !</h2> <br><br>

<%= form_tag url_for(action: 'create'), method: "post" do %>

    <%= label_tag 'Title :' %> <br>
    <%= text_field_tag 'title'%> <br><br>

    <%= label_tag 'Content :' %> <br>
    <%= text_area_tag 'content'%> <br><br>

    <%= submit_tag "Create Gossip" %>
<% end %>

我尝试对成功警报执行相同的操作,但是如果我在控制器中放置 @success = true 并在索引视图中放置 <% if @success %> 则不起作用。我没有任何想法。

如果您需要我的部分代码,请告诉我。

我试过使用 flash,但它不起作用,无论我想要什么,都使用相同的样式,并带有错误和成功警报

不要在每个表单视图中都写,你可以只写通用代码来显示与成功或错误相关的警报,然后在任何控制器中使用它:-

## app/views/layouts/application.html.erb

<html>
  <body>
    <%= render 'layouts/header' %> ## your header layout
    <div class="container">
      <% flash.each do |key, value| %>
        <div class="alert <%= flash_class(key) %>"><%= value.try(:html_safe) %></div>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %> ## your footer layout
    </div>
  </body>
</html>

根据您的要求使用帮助程序获得正确的 bootstrap 类 :-

module ApplicationHelper
    def flash_class(level)
    level = level.to_sym
    case level
        when :notice then "alert-info"
        when :success then "alert-success"
        when :error then "alert-danger"
        when :alert then "alert-warning"
    end
  end
end

然后在任何控制器中使用上面的代码:-

if @gossip.save
  flash[:success] = "Your success custom message."
  redirect_to root_path 
else
  err_msg = ""
  flash.now[:error] = @gossip.errors.full_messages.map{|msg| err_msg << "#{msg} <br> "}
  render "new"
end

Note :- Use flash when you are redirecting, and flash.now when you are rendering.

编辑内容:-

在显示之前在错误消息中添加 <br> 标记,然后在视图页面上添加 .html_safe 以在下一行显示每个错误。这个黑客应该工作,用户将能够看到与对象相关的所有错误消息。

flash.now[:error] = @gossip.errors.full_messages.map{|msg| err_msg << "#{msg} <br> "}

可见

<% flash.each do |key, value| %>
   <div class="alert <%= flash_class(key) %>"><%= value.try(:html_safe) %></div>
<% end %>

创建部分即显消息 app/views/layouts/_flash。html.erb (请给文件名加上下划线,因为它是部分的)

<% flash.each do |key, value| %> 
  <div class="alert alert-<%= key == "success" ? "success" : "danger"  %>">
    <%= value %>
  </div>
<% end %> 

将部分添加到您的 app/views/layouts/application.html.erb

<!DOCTYPE html>
<html lang="en">
<head>

</head>
<body>
  ...
  <%= render 'layouts/flash' %> 
  <%= yield %>
</Body>
</html>

在你的控制器中

def create
  @gossip = Gossip.new(title: params[:title], content: params[:content], user: User.find(182))

  if @gossip.save
    flash[:success] = 'success'
    redirect_to root_path 
  else
    flash[:danger] = @gossip.errors.full_messages[0]
    render "new"
  end
end