Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Make Posts Private


  • Please log in to reply
4 replies to this topic

#1 darkknight89

darkknight89

    Passenger

  • Members
  • 2 posts

Posted 27 March 2014 - 05:15 PM

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?



#2 Jemagee

Jemagee

    Inspector

  • Members
  • 62 posts

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



#3 darkknight89

darkknight89

    Passenger

  • Members
  • 2 posts

Posted 27 March 2014 - 05:22 PM

I already have a user model set up so that's not a problem.



#4 Jemagee

Jemagee

    Inspector

  • Members
  • 62 posts

Posted 27 March 2014 - 05:23 PM

So you write the conditional to check if the blog post is private && the user is logged in - if so - display comments - if not - don't



#5 Jamie

Jamie

    Controller

  • Moderators
  • 140 posts
  • LocationNewcastle, UK

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