views:

81

answers:

2

Hi,

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 message, 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.

A: 

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.

davidcelis
+1  A: 

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

sarnold
I don't see an issue with this. If the box is unchecked and no value is posted, the flag is nil, which does not evaluate to true. Therefore, the comment would not be evaluated as private.
davidcelis
@davidcelis, you're right, when the model object is first created. However, if the model object has had the box already checked, and the user unchecks the box, there wouldn't be a value in the param hash, and the 'check' would never be removed from the model object. The URL I pasted said modern rails works around this by including hidden form fields, but the _caveat_ is that the hidden form field thing breaks if the checkbox is accessing data through an array.
sarnold
Hmmm, I wasn't aware of this. Really useful to know. I suppose then, if the OP was to allow users to update comments and change their privacy status, a pair of radio buttons would be a better choice (I only suggest a pair since a single radio button can't be deselected)
davidcelis
so there's no way to allow the user to change the status later with a checkbox?
Ryan Max
@Ryan Max, in older Rails you needed to stuff a temporary hash with the 'unchecked' boxes, merge in the `params` hash, and _then_ you'd get your answer. Based on the URL I included in my answer, it looks like more modern Rails releases they have included some helper-code to fake it up so you can just write decent looking code, and the framework will handle the mess behind the scenes. But it can't handle the mess if you are storing the results of the checkbox in an array in your model. In that case, you probably have to do the ugly temporary array with checkboxes set to unchecked.
sarnold
are there any more specific references you can point me to? Thank you so much for all your help.
Ryan Max
@Ryan Max, the [Advanced Rails Recipes](http://www.pragprog.com/titles/fr_arr/advanced-rails-recipes) book has a nice AJAX toggle that side-steps all of this IFF javascript is working on the client: [view](http://media.pragprog.com/titles/fr_arr/code/AjaxRestToggle/app/views/errata/index.html.erb) [helper](http://media.pragprog.com/titles/fr_arr/code/AjaxRestToggle/app/helpers/application_helper.rb) [controller](http://media.pragprog.com/titles/fr_arr/code/AjaxRestToggle/app/controllers/errata_controller.rb) -- though reading the book would be easier than scouring the published source.
sarnold