Flash 通知消息未显示“ruby on rails”

The flash notice message is not displaying " ruby on rails"

我来这里之前搜索过答案,但一无所获。 我的控制器里有这个

def create
    @article = Article.new(article_params)
    if @article.save
        flash[:notice] = "Article was successfully created"
        redirect_to article_path(@article)
    else
        render 'new'
    end
end

我将其添加到 application.html.erb:

<body>
  <% flash.each do |name, msg| %>
    <ul>
      <li><%= msg %></li>
    </ul>
  <% end %>
<%= yield %>

这是我的 show.html.erb:

<h1>Showing selected article</h1>

<p>
  Title: <%= @article.title %>
</p>

<p>
  Description: <%= @article.description %>
</p>

提交表单后进入展示页面,闪现通知不显示。为什么?

我查看了你的 github 仓库,你似乎没有使用 application.html.erb 作为你的布局,因为你的文章控制器继承自 ActionController::Base

有两种方法可以做到这一点。

您可以更改您的控制器文件以继承自 ApplicationController:

class ArticlesController < ApplicationController

end

或者您可以将默认布局添加到控制器文件:

class ArticlesController < ActionController::Base
layout 'application'

end

希望它能解决您的问题。