I haven't been working with Rails for all that long and am working on creating a blog. I would like to have a selector in the post form with "Public and "Private" and when Private is selected have that post not display unless the user is signed in. What is the best way to go about doing this?
Make Posts Private
Posted 27 March 2014 - 05:18 PM
The best way to do this is in your user model/controller set up. When a user is logged in you assign a session id of some sort - your choice - easiest is session[:user]. Then you can have a check when displaying a blog to see if the session[:user] is set. Remember to 'destroy' (set it to nil) when the user logs out.
You'll need a user model for your blog if you are going to require user logins
Posted 28 March 2014 - 05:35 PM
Like what has been said above, integrate some sort of user ability. I'd consider looking at Devise or Clearance and once installed look to then adding a private boolean field to your blog posts model.
If the post is private and the user is logged in then display the post otherwise don't.
If you need any further help please do share the code so we can point you in the right direction.
Better still, if you have the code on GitHub we can possibly pull request it with our ideas.
Best of luck!
Rails developer based in Newcastle, UK.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users