2011-01-25 61 views
9

我已經在我的控制器爲什麼:通知中未顯示重定向後對Rails 3

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 

下面的代碼和我tv_shows/index.html.erb

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

但當以下我創建了一個新條目,在重定向到tv_shows_path之後,通知消息不會出現。有沒有人知道爲什麼?

回答

0

之所以代碼沒有工作與我的認證代碼的問題。經過我實現了我的身份驗證的新方法,從刮上面的代碼正在工作。

17

是否有任何理由試圖使用:notice而不是flash[:notice]

控制器:

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 

查看:

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

我覺得肖恩是正確的關於View,它應該是「閃[:聲明]」。 – 2011-01-26 00:24:53

+0

我讀過這將是與軌道版本3的新方式...但我會嘗試您的建議,謝謝! – maveonair 2011-01-26 06:36:26

+0

@Fabian:這是否適合你? – Shaun 2011-01-28 04:32:15

4

我遇到了類似的問題,原因是我重定向到本身有另一個重定向的操作。在上述情況下,最可能的原因是在tv_shows_path內存在另一個重定向。

在我來說,我有一個過濾器是這樣的:

redirect_to root_url, notice: 'Unauthorized access!' 

而且root_url被設置爲指向home#index

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

這第二個redirect_to的引起了我「unauthorized_access」通知中未現身。

解決方案是立即重定向到customers_path而不是root_url。希望這可以幫助某人。

1

我只是有同樣的問題,錯誤是如此愚蠢,但有時不可知。

我在我的應用程序佈局如下代碼:

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

現在,你可以看到爲什麼我無法看到我的提示信息?

是!如果你把=標誌在這裏您應該檢查:

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