Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • default_url_options in Rails

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 456
    Comment on it


    default_url_options in Rails

    Rails has a url_for helper, which is used for generating hyperlinks. So many times we want that a certain thing or parameter needs to be passed with each link or request to tell the controller to do certain thing. Let's take an example of locales in rails, where we want that locales are needed to be sent as a parameter with each request, that tells the controller to set a specific locale. For this kind of purpose default_url_options is used, that works as an entry point for url_for and adds a specific parameter hash with the url. You can set it in any controller you need or in ApplicatinController( to make it available everywhere). Another example can be, if we want an access_id to be sent with each url_for, then it will look like:

    class ApplicationController < ActionController::Base
      def default_url_options
        { access_id: "ACCESS_ID_VALUE" }
      end
    end
    


    The parameter is not actually passed with every request. In reallity, it is stored as a hash in cache for performance. Hope you liked this, For more click here.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: