rails notices not appearing

送分小仙女□ 提交于 2019-12-23 03:43:12

问题


I'm in Rails 4 using Bootstrap

This is my create action in my controller:

  def create
    @contest = Contest.new(contest_params) 
    if @contest.save
      flash[:notice] = "Contest saved successfully"
        redirect_to @contest
    else
        redirect_to root_path, :notice => 'Project not saved'
    end
  end

How come there is no notice when I submit the form? Is there something I am missing? I'm a beginner and have not been able to figure this out.

I have the following in my layout already:

  <body>
    <p class="notice"><%= flash[:notice] %></p>
      <p class="alert"><%= flash[:alert] %></p>

    <%= render 'layouts/header' %>

      <%= yield %>

    <%= render 'layouts/footer' %>

  </body>

回答1:


try like this:

 flash[:notice] = 'Project saved successfully'
 redirect_to about_path

redirect with flash was broken in some versions. Workaround if you want to try:

redirect_to url, :flash => { :notice => "notice" }

also edit your layout

Remove the layouts header in case it conflicts.

 <body>
     <p class="notice"><%= flash[:notice] %></p>
      <p class="alert"><%= flash[:alert] %></p>

      <%= yield %>

    <%= render 'layouts/footer' %>

  </body>



回答2:


Read about the flash. You must display the flash content in your html views, preferably in your layout.

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



回答3:


Set <%= flash[:notice] %> in root_path in your html view.



来源:https://stackoverflow.com/questions/24031396/rails-notices-not-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!