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">×</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
andj render
. - As recommended there, I have also tried
render_to_string
instead of justrender
. - 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:
- What is wrong with my current code?
- Does the approach I am trying to use make sense at all, and if not, what shoud I do instead?