Guest User

Untitled

a guest
Mar 10th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. # Add Basic Auth to Rails Controller
  2. #
  3. # AUTHOR : Tomoya Hirano
  4. # DATE : 2009-08-04
  5. #
  6. # Cf: Head First Rails p.143
  7. #
  8. class QuestionsController < ApplicationController
  9. before_filter :check_logged_in, :only => [:edit, :update]
  10.  
  11. def new
  12. @q = Question.new
  13. end
  14.  
  15. def create
  16. @q = Question.new params[:q]
  17. @q.save
  18. redirect_to "/questions/#{@q.id}"
  19. end
  20.  
  21. def edit
  22. @q = Question.find(params[:id])
  23. end
  24.  
  25. def update
  26. @q = Question.find(params[:id])
  27. @q.update_attribtes(params[:q])
  28. redirect_to "/questions/#{@q.id}"
  29. end
  30.  
  31. def show
  32. @q = Question.find(params[:id])
  33. end
  34.  
  35. def index
  36. @qs = Question.all
  37. end
  38.  
  39. def destroy
  40. @q = Question.find(params[:id])
  41. @q.destroy
  42. redirect_to "/questions/"
  43. end
  44.  
  45. private
  46. def check_logged_in
  47. authenticate_or_request_with_http_basic("Questions") do |username, password|
  48. username == 'admin' && password == 'admin'
  49. end
  50. end
  51. end
Add Comment
Please, Sign In to add comment