在我的Rails 3应用中,我希望允许用户指定其他人可以搜索其个人资料的哪些部分。如果我想让整个用户不可见,我知道如何做到这一点,但是如何设置它以便可以指定多个字段可以搜索或不单独分配?
更多信息:
在功能方面,我希望根据用户选择在/settings
中搜索的个人资料部分来限制搜索。例如,个人资料的部分内容为@user.profile.hometown
或@user.profile.current_city
。我正在使用Railscasts #52和Trevor Turk's tutorial的组合来设置其他人可以在设置中搜索复选框的内容。
当在设置中定义可搜索性时,当用户搜索(或过滤)/users
索引时,未隐藏的内容将是公开的和可搜索的。就数据库在隐藏表列或分组方面的工作方式而言,我考虑过隐藏表,但这可能不是最好的解决方案。我是初学者,并且说实话并没有真正考虑过这一点。
答案 0 :(得分:4)
因此,最直接的方式(如果只想显示/隐藏一些东西,这将起作用)只是为你需要显示/隐藏的每件事创建一个布尔列。所以,如果你有一个电话号码字段,你可以有一个名为“show_phone_number”的列,当它为true时它会显示它。
您可能需要的下一个级别是,不是显示/隐藏特定列,而是为用户的每个逻辑部分显示类似show_contact_info
,show_photos
等的show / hide布尔列会显示或隐藏。
然后在你看来,你有类似的东西:
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 />
...
最后(这里的代码未经测试,但我认为你会得到这个想法)让我们说你的网站有一个社交结构,你想向某些人展示信息,而不是向其他人展示。基本上你需要以某种形式提供以下内容:
所以,在你的User
模型中你会有类似的东西:
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
然后是一个名为ProfileVisibilities
的课程:
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
然后是一个名为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"
然后在您的控制器中,例如:
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
...
在你看来:
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 %>
...