Nadiya
Nadiya

Reputation: 1527

Rails template rendering (JS instead of HTML)

Is it possible to render only Javascript file instead of HTML for Rails action.

For example action 'index':

def index
  @products = Product.all
end

I would like the file "index.js.erb" to be called instead of "index.html.erb".

Upvotes: 1

Views: 609

Answers (1)

dnsh
dnsh

Reputation: 3633

Use respond_to .

def index
  @products = Product.all
  respond_to do |format|
    format.js
  end
end

This means client can only ask for JS response. More about MimeResponds

Upvotes: 1

Related Questions