Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Flash
- ```ruby
- # supports only notice and alert by default
- # the rest has to go into flash hash
- redirect_to :index, notice: "success"
- redirect_to :new, notice: "errors"
- redirect_to :new, flash: { success: "yeah" }
- flash[:info] = "updated"
- flash.now[:notice] = "wrong params" # sets the flash data in the current request
- flash.keep # keeps the flash data for the next request
- ```
- ## Callbacks
- ```ruby
- before_action :do_authentication # calls the method
- after_action :do_compresssion
- around_action :do_something # your method needs to call yield
- skip_before_action
- skip_after_action
- skip_around_action
- before_action :do_authentication, only: [:create, :delete]
- before_action :do_authentication, except: [:create, :delete]
- ```
- ## default_url_options
- - Sets the default URL params
- - Will be overriden by params passed in
- ```ruby
- class ApplicationController < ActionController::Base
- def default_url_options
- { locale: I18n.locale }
- end
- end
- ```
- ## Set response
- ```ruby
- respond_to :json # and works with respond_with Products.all
- respond_to do |format|
- format.html { render :index }
- format.json { render xml: @products.to_json }
- format.js { render 'partials' } # for AJAX calls with button_to remote: true
- format.xml { render xml: @products.to_xml
- ```
- ## Filtering for authentication
- ```ruby
- class ApplicationController < ActionController::Base
- before_action :abacus_authenticate, except: :login
- def login
- # do login
- end
- def abacus_authenticate
- render text: 'Unauthorized Access', status: '401' if session[:user_id].nil?
- end
- end
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement