1

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".

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

It results in "ActionController::UnknownFormat" error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.