ruby-on-rails-3 – 为什么:注意在Rails 3中重定向后不显示

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 为什么:注意在Rails 3中重定向后不显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的控制器中有以下代码
  1. def create
  2. @tv_show = TvShow.new(params[:tv_show])
  3.  
  4. respond_to do |format|
  5. if @tv_show.save
  6. format.html { redirect_to(tv_shows_path,:notice => 'Tv show was successfully created.') }
  7. format.xml { render :xml => @tv_show,:status => :created,:location => @tv_show }
  8. else
  9. format.html { render :action => "new" }
  10. format.xml { render :xml => @tv_show.errors,:status => :unprocessable_entity }
  11. end
  12. end
  13. end

和我的tv_shows / index.html.erb中的以下内容

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

但是当我创建一个新条目时,通知消息不会在重定向到tv_shows_path之后出现.有没有人想到为什么?

解决方法

有什么理由你试图使用:通知而不是闪光[:通知]?

控制器:

  1. respond_to do |format|
  2. if @tv_show.save
  3. format.html {
  4. flash[:notice] = 'Tv show was successfully created.'
  5. redirect_to tv_shows_path
  6. }
  7. format.xml { render :xml => @tv_show,:location => @tv_show }
  8. else
  9. format.html { render :action => "new" }
  10. format.xml { render :xml => @tv_show.errors,:status => :unprocessable_entity }
  11. end
  12. end

视图:

  1. <% if flash[:notice] %>
  2. <div id="notice"><%= flash[:notice] %></div>
  3. <% end %>

猜你在找的Ruby相关文章