Guest User

Untitled

a guest
Apr 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. Posts controller...
  2. ...
  3. #I want to allow only the post owner to be able to view "show" their own post(s)
  4.  
  5. ...
  6.  
  7. # this will work
  8. def show
  9. @post = current_user.posts.find(params[:id])
  10. if @post.nil?
  11. flash[:notice] = 'Access Denied'
  12. redirect_to :action => 'index'
  13. return
  14. end
  15. respond_to do |format|
  16. format.html # show.html.erb
  17. format.xml { render :xml => @post }
  18. end
  19. end
  20.  
  21. # I would do this though
  22. def show
  23. @post = current_user.posts.find(params[:id])
  24. (render(:nothing => true, :status => 403) && return) if @post.nil?
  25. respond_to do |format|
  26. format.html # show.html.erb
  27. format.xml { render :xml => @post }
  28. end
  29. end
  30.  
  31. ... other basic crud methods
  32.  
  33. ...
Add Comment
Please, Sign In to add comment