Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. ## Flash
  2.  
  3. ```ruby
  4. # supports only notice and alert by default
  5. # the rest has to go into flash hash
  6. redirect_to :index, notice: "success"
  7. redirect_to :new, notice: "errors"
  8. redirect_to :new, flash: { success: "yeah" }
  9. flash[:info] = "updated"
  10.  
  11. flash.now[:notice] = "wrong params" # sets the flash data in the current request
  12. flash.keep # keeps the flash data for the next request
  13. ```
  14.  
  15. ## Callbacks
  16.  
  17. ```ruby
  18. before_action :do_authentication # calls the method
  19. after_action :do_compresssion
  20. around_action :do_something # your method needs to call yield
  21.  
  22. skip_before_action
  23. skip_after_action
  24. skip_around_action
  25.  
  26. before_action :do_authentication, only: [:create, :delete]
  27. before_action :do_authentication, except: [:create, :delete]
  28. ```
  29.  
  30. ## default_url_options
  31. - Sets the default URL params
  32. - Will be overriden by params passed in
  33.  
  34. ```ruby
  35. class ApplicationController < ActionController::Base
  36. def default_url_options
  37. { locale: I18n.locale }
  38. end
  39. end
  40. ```
  41.  
  42. ## Set response
  43.  
  44. ```ruby
  45. respond_to :json # and works with respond_with Products.all
  46.  
  47. respond_to do |format|
  48. format.html { render :index }
  49. format.json { render xml: @products.to_json }
  50. format.js { render 'partials' } # for AJAX calls with button_to remote: true
  51. format.xml { render xml: @products.to_xml
  52. ```
  53.  
  54. ## Filtering for authentication
  55.  
  56. ```ruby
  57. class ApplicationController < ActionController::Base
  58.  
  59. before_action :abacus_authenticate, except: :login
  60.  
  61. def login
  62. # do login
  63. end
  64.  
  65. def abacus_authenticate
  66. render text: 'Unauthorized Access', status: '401' if session[:user_id].nil?
  67. end
  68. end
  69. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement