【问题标题】:How to create alert with redirect_to如何使用 redirect_to 创建警报
【发布时间】:2020-02-22 21:21:17
【问题描述】:

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

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

这是我的八卦控制器:

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 并在索引视图中放置 &lt;% if @success %&gt; 则不起作用。我没有任何想法。

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

我已经尝试过使用 flash 但这不起作用,并且我想要与错误和成功警报相同的样式

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap rest alert crud


    【解决方案1】:

    创建 Flash 消息部分 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
    

    【讨论】:

      【解决方案2】:

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

      ## 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>
      

      根据您的要求使用帮助器获取正确的引导类:-

      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
      

      注意:- 重定向时使用 flash,渲染时使用 flash.now。

      编辑内容:-

      在显示之前在错误消息中添加&lt;br&gt; 标记,然后在查看页面添加.html_safe 以在下一行显示每个错误。这个 hack 应该是可行的,用户将能够看到与对象相关的所有错误消息。

      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 %>
      

      【讨论】:

      • 我已经成功实现了 flash 的成功,但是如何包含 @gossips.errors.full.messages.each 等...?对于错误方式
      • @FFouquet42 我刚刚编辑了我的答案。你可以试试这个,它会起作用的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2011-11-12
      • 2021-12-12
      相关资源
      最近更新 更多