Search code examples
ruby-on-railsrubyrjs

How to reload partial pages in Rails using RJS?


Hi Currently i am working on a rails project . where i have to reload a partial page .

For example Create action

 def create
    # Some transaction to the database 
   respond_to do |format|
     format.html {render :text => 'Add post' }
     format.js
   end
  end

In my views posts/create.js.rjs i would like to reload the users/_login.html.erb file This contains the total number of posts or the post's size , when it creates a new post its should reload the partial page _login.html.erb and display the posts incremented size

In create.js.rjs if i use page.reload it reloads the entire page , How to reload only the partial file _login.html.erb inside create.js.rjs .


Solution

  • The code will look like:

      page.replace_html "login-block-id", :partial => "users/login"
    

    Where login-block-id is the id of element that wraps your login template

    Note that replace_html can accept the same arguments as render do.