0

My Problem

I am developing a Rails site and would like to use Javascript to render partials in some of my views depending on certain conditions. For instance-- I'd like to have a partial view of a Devise log-in/sign-in prompt come up in a modal box if the user is not signed in when accessing certain pages.

I've had a lot of trouble figuring this out-- the first issue was that I tried using render in the asset pipeline which after some research found doesn't work to begin with.

I then tried putting a js.erb file into my public/javascripts folder but javascript_include_tag force appends '.js' to the file name and using regular src=/javascripts/... didn't render the '.erb' stuff but rather it would append the text <%=j render :partial ... %>

My Solution

I have come up with this solution here and would like to know if there is a better solution to keep clean code. I am going to have a few other Javascripts that will render the same thing over different views.

I have created a app/views/shared/javascripts directory where I will put [filename].html.erb files.

To get the Javascript to run correctly I will <%= render :partial => 'shared/javascripts/...' %> wherever I want that script to be run.

Inside that script is something like:

<script>
    $(document).ready(function() {
        ...
        $(.class).append("<%=j render :partial => 'shared/modal' %>");
        ...
    });
</script>

Is There a Better Way?

As far as I can tell-- this will do what I want it to do. I'm just afraid that I'm looking at this all wrong. I'll be working on another part of the app for a while and I really hope to either verify that this is acceptable and decent or find the proper way to ensure that I can use ERB in my JS files.

5
  • why are you using js for rendering partials? Commented Aug 25, 2014 at 16:56
  • This may be one of those things where I stayed up well too late in a brain fog and got stuck on an idea of how to do things-- but I am using jQuery to bring up the Modal box. So I have my view where I sometimes want the modal-- that is where I would include the shared/javascripts/my.html.erb which has the script tags-- the JS in that file then appends the modal (another html.erb partial with the HTML for the modal box) to the view. Writing it out like that sounds kind of ridiculous. Is there a better solution? Commented Aug 25, 2014 at 17:11
  • yeah absolutely why aren't you using ruby conditional statements it's much more simpler and cleaner. Commented Aug 25, 2014 at 17:16
  • I am. In the view I only call to render the partials given a certain condition ie. customer_logged_in? Devise helper. My Javascript is explicitly for the purpose of handling DOM manipulation by taking pre-written HTML code and sticking it in my view. Commented Aug 25, 2014 at 17:18
  • your question says would like to use Javascript to conditionally render partials so can you explain what exactly is the purpose of using js? Will be able to help you better after that Commented Aug 25, 2014 at 17:22

2 Answers 2

1

I know this answer is 3 years late for the OP, but I found this question googling something else and thought I'd answer it as it's something we do quite a lot in our legacy app.

Suppose you want to render the javascript alert("Hello, world!"). You can create the partial _greetings.js.erb in the folder app/views/my_resources:

alert('<%= @msg %>')

And call it from a controller action:

@msg = "Hello, world!"
render partial: 'my_resources/greetings', formats: :js

Hope that helps someone.

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

Comments

0

One way to do this is to put your partial in your javascript and render it using something like Mustache. The problem with this approach is that if you're rendering a partial you're also rendering traditionally elsewhere in the Rails app, the Mustache template could easily get out of sync with the Rails partial.

Another way to do this would be to have an action which returns the rendered partial as a string, either as XML or wrapped in JSON, and use AJAX to request the rendered partial from that action. Then your Rails controller will handle both rendering the template (using render_to_string) and wrapping it in JSON for your javascript to consume and redisplay.

1 Comment

Thanks for the suggestions. What I decided to do is to just put my Javascript in <script> tags in html.erb partials. It isn't the prettiest solution but it's working well so far. I looked at Mustache a while back-- I'll look at that again. Thanks

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.