2013-02-21 417 views
0

我希望能夠在登錄後立即將用戶重定向到其他主頁(此部分正在工作),但我仍然希望登錄的用戶能夠訪問如果他們想要索引飼料。目前,他們只能看到favourites_show_path,而不能看到@guidelines = Guideline.order(:title).all。有沒有辦法(無需構建第二個索引操作和視圖)在登錄後返回主頁以直接訪問favourites_show_path,但仍然能夠看到@guidelines = Guideline.order(:title).all?用戶登錄後頁面重定向

def index 
    if params[:search].present? 
    @search = Sunspot.search(Guideline) do 
     fulltext params[:search] 
    end 
    @guidelines = @search.results 
    else 
    redirect_to favourites_show_path, :action => 'index' and return if current_user 
    @guidelines = Guideline.order(:title).all 
    end 
+1

沒有。他們將永遠被重定向。你不能在登錄操作上做重定向嗎? – jvnill 2013-02-21 01:15:01

+0

我使用設計 - 你可能是對的,我應該在另一個行動上做這件事,因爲我仍然希望索引行動起作用,因爲它是 – tessad 2013-02-21 01:33:45

回答

0

只需推動@guidelines...出的條件,並重新命名的搜索結果符號:

def index 
    @guidelines = Guideline.order(:title).all 
    if params[:search].present? 
    @search = Sunspot.search(Guideline) do 
     fulltext params[:search] 
    end 
    @results = @search.results 
    else 
    redirect_to favourites_show_path, :action => 'index' and return if current_user 
    end 
end 
+0

謝謝,但這似乎並不奏效。我不確定爲什麼搜索結果符號需要更改。重點在於,如果你搜索過了,那麼符號準則將只是搜索結果,否則符號準則將成爲所有準則。您提到的方式仍會將用戶重定向到favourites_show_path(如果他們已登錄),因此不會允許他們查看符號準則(除非他們運行搜索)。 – tessad 2013-02-21 01:24:24

+0

我明白了。所以'@指南'是一切或搜索結果?他們將顯示在'favourites_show_path'上? – 2013-02-21 01:29:28

+0

是的,@guidelines是一切或搜索結果。它們將顯示在guidelines_path上。立即登錄後,我希望它指向favourites_show_path(它目前的做法),但我也想要current_user能夠看到guidelines_path(對於所有指南),如果他們想。 – tessad 2013-02-21 01:32:34

0

謝謝,我已經解決了這一點。建議我應該將重定向添加到登錄操作。所以,我從我的指標作用,除去

redirect_to favourites_show_path, :action => 'index' and return if current_user 

,而是加入到我的application_controller.rb

def after_sign_in_path_for(resource) 
favourites_show_path 
end 
相關問題