1

Suppose I make an ajax a call, from which I want to get some static template (app/views/static/some_template.html.erb), on which I want to act with some javascript stored here app/views/layouts/sign_in.js.erb. Is it possible to render multiple file ? (because I want to keep separate my js files and my html files)

def ajax_call
   respond_to do |format|
    ...
    format.js {render 'layouts/sign_in.js.erb'}
   end
end

Edit : here's my controller

    respond_to do |format|
       format.js {render 'devise/sessions/new.html.erb'}
    end

In devise/sessions/new.html.erb, I put

<div> test</div>
<%= render "layouts/sign_in.js.erb" %>

and in layouts/sign_in.js.erb, I put console.log('test');

2 Answers 2

2

You can put renders inside the partial and segment your files one more level:

def ajax_call
   respond_to do |format|
    ...
    format.js {render 'layouts/grouped_sign_in.js.erb'}
   end
end

# in _grouped_sign_in.js.erb
<%= render 'layouts/sign_in.js.erb' %>
<%= render 'layouts/create_account.js.erb' %>
Sign up to request clarification or add additional context in comments.

7 Comments

In fact, I put a console.log(test) inside my sign_in.js.erb file, but it seems not to be evaluated as javascript but instead as plain text. How can it be ?
It still doesn't work I added <%= javascript_tag do %>console.log("test");<% end %> and I get <script> //<![CDATA[ console.log("test"); //]]> </script>
What is the source code? Can you pastebin it please? I think that there is nested <script> tags, 1 global with 2 inside.
I reduced the html part of the template to <div>test</div> and it still get the same problem...
Perhaps it is related to the fact that I render in my controller some static page format.js {render 'static/new.html.erb'} (I put at the bottom of this page <%= render 'layouts/sign_in.js.erb' %> )
|
1

What you can do is the following

In your controller

@template = ActionView::Base.new('app/views/static', {}, ActionController::Base.new).render(file: 'new').to_s

respond_to do |format|
    format.js {render 'layouts/sign_in.js.erb'}
end

and then get your @template variable back in your sign_in.js.erb, <%= escape_javascript(@template) %>

Comments

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.