方法1 - 表示/非表示特定の列
だから、最も直接的な方法(とあなたが/非表示を見せたいもののほんの一握りがある場合、これは動作しますが)、単にブール列を作成することですあなたが表示/非表示にする必要があるすべてのために。したがって、電話番号フィールドを持っていれば、 "show_phone_number"という列を持つことができ、真の場合はそれを表示します。
方法2 - あなたが必要な場合があります表示/非表示セクション全体
次のレベルは、むしろ特定の列を表示/非表示のではなく、持っているブール列にそれぞれのためなどshow_contact_info
のようなもの、show_photos
を、隠す/あなたのショーユーザーが表示または非表示にする論理セクション。
次に、あなたのビューで、あなたのようなものがあるだろう:
app/views/user/show.html.erb
(または.haml
または使用しているものは何でも)を
....
<% if @user.show_contact_info %>
<%= render :partial => "user_contact_info", :locals => {:user => @user} %>
<% end %>
app/views/partials/_user_contact_info.html.erb
<%=h user.email %><br />
<%=h user.phone_number %><br />
<%= user.blog_url %><br />
...
方法3 - ショー誰がそれを見ているかに基づいてセクションを非表示/非表示にする
最後に(ここのコードはテストされていませんが、あなたはそのアイデアを得るでしょう)あなたのサイトは社会構造を持ち、他の人には情報を表示したくないとしましょう。基本的には何らかの形で次のように必要があります(どのようなセクションを表示することができます)
- セクションの可視性
- 役割(友人、フォロワー、パブリック、プライベート)
- これらを作るためのいくつかのメソッドを
class User < ActiveRecord::Base
has_many :friends, :through => "friendships" # or whatever construct you have
has_many :followers, :through => "followings" # or whatever construct you have
has_many :profile_visibilities
...
def is_friends_with(user)
friends.include?(user)
end
def is_a_follower_of(user)
user.followers.include?(self)
end
def can_see(visibilities)
visibilities.each do |v|
v.user == self || v.is_public || can_see_because_we_are_friends(v) || can_see_because_i_follow(v)
end
end
private:
def can_see_because_we_are_friends(visibility)
visibility.is_friend && is_friends_with(visibility.user)
end
def can_see_because_i_follow(visibility)
visibility.is_follower && is_follower_of(visibility.user)
end
end
:だから
を理解するには、clear /簡単な関係は、あなたのUser
モデルでは、あなたのようなものを持っていると思います
はその後ProfileVisibilities
というクラス:
app/controllers/users_controller.rb
...
def show
@user = User.find(params[:id])
@contact_info_visibilities = ProfileVisibilities.find(:all, :conditions = ['user_id = ? AND profile_section = "contact_info"', @user.id]
@photo_visibilities = ProfileVisibilities.find(:all, :conditions = ['user_id = ? AND profile_section = "photos"', @user.id]
# ... and more for each section visibility you need
end
...
そして中:次に、表のようなもの、あなたのコントローラに続いてprofile_visibilities
id | user_id | profile_section | visibility
----------------------------------------------
1 | 1 | contact_info | public # <= visible to everyone
2 | 1 | personal_info | friends # <= visible only to "friends"
3 | 1 | blog_posts | friends # <= visible to "friends"
4 | 1 | blog_posts | followers # <= ...and followers
5 | 1 | photos | friends # <= visible only to "friends"
を
class ProfileVisibilities < ActiveRecord::Base
belongs_to :user
...
def is_public
visibility == "public"
end
def is_friend
visibility == "friends"
end
def is_follower
visibility == "followers"
def is_private
!is_public && !is_friend && !is_follower
end
end
と呼ばれますあなたのビュー:
app/views/user/show.html.erb
...
<% if current_user.can_see(@contact_info_visibilities) %>
<%= render :partial => "user_contact_info", :locals => {:user => @user}
<% end %>
<% if current_user.can_see(@photo_visibilities) %>
<%= render :partial => "user_photos", :locals => {:user => @user}
<% end %>
...
情報は、質問に追加!私は今私のコメントを削除します。 – tvalent2