0

私はソーシャル ネットワーキング サイト (基本的には、正直に言うと Facebook のコピーです...) に取り組んでおり、インソシの大部分を再利用しました。しかし、インソシのフィードは、私の好みには十分に正確ではありません. そのように、より専門的なメッセージはサポートされていません。次のコードで私が何を意味するかがわかります。

item = activity.item
relationship = relationship(item)
case relationship
   when 1
     raw %(<p>You wrote on your own wall: <br/>
     #{truncate(item.body, :length => 20)}</p>)
   when 2
     raw %(<p>#{link_to item.user.name, item.user} wrote on your wall</p>)
   when 3
     raw %(<p>#{link_to item.user.name, item.user} wrote on his wall</p>)
   when 4
     raw %(<p>You wrote on #{link_to item.user.name, item.user}'s wall</p>)
   when 5
     raw %(<p>#{link_to item.user.name, item.user} wrote on 
              #{link_to item.contact.name, item.contact}'s wall</p>)
end

    def relationship(item) 
        unless item.owner.nil?
          contact = item.owner #so that it works for posts as well
        else
          contact = item.contact
        end
        user = item.user

        if current_user != contact or current_user != user
          return 5
        else
          if current_user == contact
            if current_user == user
              return 1
            else
              return 2
            end
          else
            if contact == user
              return 3
            else
              return 4
            end
          end
        end
end

私はさまざまな種類のアイテムを持っています。通常、アイテムには「ユーザー」と「連絡先」があります。投稿を除き、投稿には「ユーザー」と「所有者」がいます。投稿の他の人が誰かの壁にそれを書くことができるからです(したがって、所有者)。

連絡先をitem.contactに設定しようとするとすぐに問題が発生します... item.contactが存在しないという「NoMethod」エラーが発生し続けます。(アイテムが投稿であり、「接続」または同等のものでない場合、これは明らかです)。

だから私はあなたの意見を求めています: 1) もう少しルビーで問題を修正するか、2) 投稿モデルを変更して、投稿に「ユーザー」と「連絡先」があるようにしますか?

みんなありがとうステファノ

4

2 に答える 2

0

Rubyコードで修正します。

contact = item.contact if item.respond_to? :contact

Respond_to を使用して? これは、連絡先を持つすべてのクラスで機能します。

于 2010-09-17T12:04:41.797 に答える
0

あなたの論理によれば、リレーションシップ 3 と 4 は決して返されません。私はあなたが持っていると思います、あなたは持っているcurrent_user != contact or current_user != userつもりandでした。個人的には、最初の条件が false の場合に短絡するため、常に && を使用します。ただし、私のリファクタリングでは、他のケースが一致しない場合は 5 を返すため、必要ありません。

リレーションシップ ロジックを Item モデルに移動し、ヘルパーを適切に更新しました。

ビュー ヘルパー:

case item.relationship_to_user(current_user)
when 1
  raw %(<p>You wrote on your own wall: <br/>
  #{truncate(item.body, :length => 20)}</p>)
when 2
  raw %(<p>#{link_to item.user.name, item.user} wrote on your wall</p>)
when 3
  raw %(<p>#{link_to item.user.name, item.user} wrote on his wall</p>)
when 4
  raw %(<p>You wrote on #{link_to item.user.name, item.user}'s wall</p>)
when 5
  raw %(<p>#{link_to item.user.name, item.user} wrote on 
  #{link_to item.contact.name, item.contact}'s wall</p>)
end

アイテム クラス

class Item < ActiveRecord::Base

  def relationship_to_user(current_user)
    contact = owner || contact  

    return 1 if current_user == contact && current_user == user
    return 2 if current_user == contact
    return 3 if current_user != contact
    return 4 if current_user != contact && contact != user

    return 5
    # return 5 if current_user != contact or current_user != user
  end

end
于 2010-09-17T23:17:21.007 に答える