Rails: Render partials via ajax without having to load views

Normally, we render partials via views by using render [parial_without_underscore]. But we can also render partials in our document using ajax. To do this we need to

  1. Create an action method that we would call to load the partial and write the following code inside the action method

    /**

    *layout: false will disable the loading of layouts and headers we create

    *partial: “popup_form” will load the parial “_popup_form.html.erb” file

    *

    *@author Shiva Bhusal

    */

    respond_to do |format|
    format.html { render partial: "popup_form", layout: false }
    end

  2. Now make an ajax call to url controller/action/
  3. Grab the data via HTTP response and render in the webpage

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s