Why is :notice not showing after redirect in Rails 3

…衆ロ難τιáo~ 提交于 2020-01-12 14:29:33

问题


I have the following code in my controller

  def create
    @tv_show = TvShow.new(params[:tv_show])

    respond_to do |format|
      if @tv_show.save
        format.html { redirect_to(tv_shows_path, :notice => 'Tv show was successfully created.') }
        format.xml  { render :xml => @tv_show, :status => :created, :location => @tv_show }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @tv_show.errors, :status => :unprocessable_entity }
      end
    end
  end

and the following in my tv_shows/index.html.erb

<div id="notice"><%= notice %></div>

but when I create a new entry the notice message does not appear after the redirect to tv_shows_path. Have anyone an idea why?


回答1:


Is there any reason you're trying to use :notice and not flash[:notice]?

Controller:

 respond_to do |format|
  if @tv_show.save
    format.html { 
      flash[:notice] = 'Tv show was successfully created.'
      redirect_to tv_shows_path 
    }
    format.xml  { render :xml => @tv_show, :status => :created, :location => @tv_show }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @tv_show.errors, :status => :unprocessable_entity }
  end
end

View:

<% if flash[:notice] %>
    <div id="notice"><%= flash[:notice] %></div>
<% end %>



回答2:


I encountered similar 'problem' and the cause was that I was redirecting to action that in itself had another redirection. In the above case the most probable cause was that within tv_shows_path another redirection exists.

In my case I had something like this in a filter:

redirect_to root_url, notice: 'Unauthorized access!'

And root_url was set to point to home#index:

# Home controller
def index
  if user_signed_in? && current_user.admin?
    redirect_to users_path
  else
    redirect_to customers_path
  end
end

This second redirect_to was causing my 'unauthorized_access' notice not to show up.

The solution is to simply redirect to customers_path immediately and not to root_url. Hope this helps someone.




回答3:


I just had the same issue and the error was so silly but sometimes unnotable.

I have in my application layout the following code:

<div id="content" >
    <div class="wrapper">
        <% flash.each do |name, msg| %>
            <% content_tag :div, msg, class: "flash #{name}"%>
        <% end %>
        <%= yield %>
    </div>
</div>

Now, can you see why I was unable to see my flash message?

Yup! You should check if you put the = sign here:

<%= content_tag :div, msg, class: "flash #{name}"%>



回答4:


The reason why the code didn't work was a problem with my authentication code... After I implemented my new way of authentication from scratch the code above is working.



来源:https://stackoverflow.com/questions/4799395/why-is-notice-not-showing-after-redirect-in-rails-3

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