简体   繁体   中英

The flash notice message is not displaying “ ruby on rails”

I searched for an answer before I came here but found nothing. I have this in my controller

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

and I add this in my application.html.erb:

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

and here is my show.html.erb:

<h1>Showing selected article</h1>

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

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

After submitting the form I go to the show page,the flash notice does not display. why?

I have look into your github repo, it seems that you are not using application.html.erb as your layout, because your article controller is inherited from ActionController::Base

There are 2 ways you can do this.

You can either change your controller file to inherit from ApplicationController:

class ArticlesController < ApplicationController

end

Or you can add default layout to the controller file:

class ArticlesController < ActionController::Base
layout 'application'

end

Hope it solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM