我想弄清楚如何使用助手文件。Rails 4 - 如何在父視圖中使用助手方法
我有項目和潛在用途的模型。該協會是:
項目
has_many :potential_uses
accepts_nested_attributes_for :potential_uses, reject_if: :all_blank, allow_destroy: true
可能的用途
belongs_to :project
belongs_to :user
在我的項目展示頁面,我要在我的項目幫助文件夾中定義一個輔助方法的引用:
<%= potential_uses_for_viewer(@project) %>
輔助方法(在projects_helper.rb中)有:
def potential_uses_for_viewer(project)
if @current_user.id == @project.profile.user_id
render 'potential_uses/private_comments'
else
render 'potential_uses/open_comments'
end
end
我在我潛在的使用視圖文件夾中有兩個視圖部分。如果當前用戶是項目創建者,我想呈現其中一個部分,而如果當前用戶不是項目創建者,則想呈現其中一個部分。
目前,沒有渲染錯誤,但項目文件夾中的助手不起作用。無論我是否以進行該項目的用戶身份登錄,我都可以看到potential_uses/open_comments
視圖。
任何人都可以看到我出錯了嗎?
幫助程序中的@ current_user是什麼?您是否嘗試將其打印到控制檯? –
@ArunKumar - 這是一種設計方法,可以查看登錄用戶是誰。 – Mel
我想由devise提供的方法是'current_user'。我的答案能解決你的問題嗎? –