2

Rails 3アプリを構築しています。認証ロジックを管理するためにdeviseを使用していますが、現在のユーザーが関連付けを持つオブジェクト(データベースへのレコード)を所有しているかどうかを確認するヘルパーメソッドがあるかどうか疑問に思っていました "has_many "。

例:

User

has_many :reports 

私はこの状況を持っています:

<%=  current_user.id == report.user.id ? "Personal Report" : report.user.username %>

次のようなものを作成できます。

<%= owned ? "yes" : "no" %>

ありがとう

4

2 に答える 2

13

ヘルパー:

def owned(report)
    current_user.id == report.user_id
end

使用法:

<%= owned(report) ? "yes" : "no" %>
于 2013-01-28T16:28:58.163 に答える