Rails Private / Public Message Toggle
I'm not quite sure how to go about framing this question, but I am building a social network from the ground up using rails; one feature is twitter style messages. I am trying to create an option that when the user submits their me开发者_开发百科ssage, the have a checkbox that says "private". If the checkbox is checked, only the users friends can see the submission. If the checkbox is checked, any user can see the questions.
How should I go about implementing this? I don't even know where to look for an even vaguely similar idea or strategy. Any advice would be much much appreciated.
You can use a radio-button or a check-box for this.
Because check-box values aren't sent along with HTML form submissions if the check-box is unchecked, there are some caveats you should know about using check-boxes: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M002298
This can be done easily by giving a message a boolean "private" attribute. When it's set to true (submitting the form with the box checked), only friends can view it. If it's false (or nil), anybody can see it. Then, it's just a matter of having a method such as...
def is_allowed_to_view?(comment)
current_user.is_friends_with(comment.creator) || !comment.private
end
in your User.rb
model, or some other method of checking privacy for a comment.
精彩评论