2012-05-22 55 views
0

我有這樣一些現有的航線有:如何根據條件爲我的Rails視圖中的鏈接添加前綴?

/people/:id 
/events/:id 

等等,在我routes.rb

現在我想爲我的會員客戶提供自定義樣式。我目前的想法是,我的路線將如下所示:

/people/:id 
/events/:id 
... 

/affiliates/:id/people/:id 
/affiliates/:id/events/:id 
... 

等等。

但是,我想確保我在我的視圖中生成的鏈接始終以/affiliates/:id爲前綴,前提是我在聯屬網頁上。 (我不希望每次使用url_forpath_for幫手時都必須寫一個「這是聯屬計劃嗎?」。)

要做到這一點,最好的方法是什麼?

回答

0

在我的選擇中,如果您在控制器內而不是在視圖中處理該邏輯,則會更好。

你可以有一些如下:

class EventController < ApplicationController 
    . 
    . 
    def show 
    @event = Event.find(params[:id]) 
    if(current_user.is_affiliate?) 
     #render the affiliate view 
    else 
     #renders regular view 
    end 
    end 

在你看來,你可以簡單地有

<%= link_to "Event", @event %> 

這樣的觀點並不需要管理這種邏輯,你可以保持它控制器。

相關問題