2013-12-11 30 views
1

我在我的視圖一些代碼,如果用戶使用它是應用程序控制器上的方法記錄在一個測試:德雷珀裝飾和使用的應用控制器上的方法

def logged_in? 
    !!current_user 
end 

我用這個方法上的圖只有當用戶登錄顯示特定的信息。

<%= **logged_in?** ? link_to('+', upvote_post_path(post), method: 'post', remote: true) : '+' %> 
<span id = "post-<%= post.slug %>-votes"> <%= post.total_votes %></span> 

現在,如果我想這個邏輯轉移到裝飾,因爲裝飾不能應用控制器上訪問此方法將無法正常工作。我需要這個在那裏。所以我的問題是:處理這種情況的最佳方法是什麼?該邏輯可以是來自應用程序助手的訪問。有沒有一種方法,我的裝飾對象訪問此方法而不重複該方法兩次?

+0

你在裝飾:兩個軌 提供助手和那些在你的應用程序定義可以在 通過h方法進行訪問的設計師嗎? –

+0

你使用這個gem https://github.com/drapergem/draper,用'h'作爲方法調用的前綴。像這樣h.logged_in? – nickcen

+0

幾分鐘前有人實際回答了這個問題,但帖子被刪除了。我不知道爲什麼......但是,解決方案是在方法之前加上「h」。在我的情況下,這將是「h.logged_in?」謝謝! – DMiller

回答

3

你可以從Draper

普通Rails的helper訪問幫手h.some_methods

def link_to_edit_event 
    h.link_to h.t('buttons.edit'), h.edit_event_path(id) 
end 

仍然是大量的任務非常有用。

class ArticleDecorator < Draper::Decorator 
    def emphatic 
    h.content_tag(:strong, "Awesome") 
    end 
end