1

In my Rails 4 app, I am trying to display some views as modals (with Bootstrap modal), instead of the regular .html.erb views.

For instance, I have a calendar model, with a custom analysis action in the controller:

def analysis
  @calendar = Calendar.find(params[:id])
  respond_to do |format|
    format.js
  end
end

Following what is explained in this tutorial and that other tutorial, I am trying to create the following files:

# _dialog.html.erb

<div class="modal fade" id="dialog" tabindex="-1" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" 
                data-dismiss="modal" 
                aria-label="Close">
                <span aria-hidden="true">&times;</span></button>
        <h3 class="modal-title"></h3>
      </div>
      <div class="modal-body">
      </div>

    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

# _analysis.html.erb

<p><%= @calendar.name %> Analysis</p>

# analysis.js.erb

// Add the dialog title
$('#dialog h3').html("Calendar Analysis");

// Render calendar analysis
$('.modal-body').html('<%= j render(:partial => 'calendars/analysis') %>');

// Show the dynamic dialog
$('#dialog').modal("show");

// Set focus to the first element
$('#dialog').on('shown.bs.modal', function () {
    $('.first_input').focus()
  })

Last but not least, I call the modal from a _heading.html.erb partial, rendered inside the calendar show.html.erb view, with the <%= render 'analysis' %> helper and the following link:

<%= link_to '<i class="glyphicon glyphicon-dashboard"></i>'.html_safe, calendar_analysis_path, :remote => true, 'data-toggle' =>  "modal", 'data-target' => '#modal-window' %>

When I launch my app, I then get the following error:

undefined method `render' for #<#<Class:0x007fee248d8118>:0x007fee23577ce0>

—————

UPDATE: I have tried different ways to render the partial:

  • As recommended here, I tried both escape_javascript render and j render.
  • As recommended there, I have also tried render_to_string instead of just render.
  • As recommended in that other question, I even tried different ways to call the partial, both with render(:partial => 'calendars/analysis') and (render 'calendars/analysis').

But none of these solutions worked.

—————

I have done quite a lot of research on that topic and I have to say that I am confused now.

While both tutorials mentioned above recommend this approach, other sources, including this one, point out that you cannot render a partial from assets in Rails.

Therefore:

  1. What is wrong with my current code?
  2. Does the approach I am trying to use make sense at all, and if not, what shoud I do instead?
2
  • 1
    does your analysis.js.erb live somewhere under the views directory? Commented Nov 6, 2015 at 0:39
  • Thanks for your comment. analysis.js.erb is currently under app/assets/javascript. Could that cause the problem? Commented Nov 6, 2015 at 0:45

1 Answer 1

2

The code under app/assets can not call render. Actually it can not call almost anything except a very few methods.

Move it somewhere to app/views/somethings/analysis.js.erb

Where somethings is the name of your controller in plural form, so just put it near the _analysis.html.erb.

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

1 Comment

You were perfectly correct, this worked like a charm. Thank you very much, I was really struggling with this.

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.