在我的Rails 4應用程序中,我試圖顯示一些視圖爲modals
(使用Bootstrap模式),而不是常規的.html.erb
視圖。Rails 4:escape_javascript呈現部分不工作
舉例來說,我有一個calendar
模式,與控制器定製analysis
動作:
def analysis
@calendar = Calendar.find(params[:id])
respond_to do |format|
format.js
end
end
下面是什麼在this tutorial和that other tutorial解釋,我想創建以下文件:
# _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()
})
最後但並非最不重要的,我稱之爲從_heading.html.erb
部分,日曆show.html.erb
視圖中呈現的模式,與<%= render 'analysis' %>
助手和以下鏈接:
<%= link_to '<i class="glyphicon glyphicon-dashboard"></i>'.html_safe, calendar_analysis_path, :remote => true, 'data-toggle' => "modal", 'data-target' => '#modal-window' %>
當我啓動我的應用程序,然後我得到了以下錯誤:
undefined method `render' for #<#<Class:0x007fee248d8118>:0x007fee23577ce0>
-----
更新:我已經嘗試了不同的方式來呈現部分:
- As recommended here,我都嘗試
escape_javascript render
和j render
。 - As recommended there,我也試過
render_to_string
而不是render
。 - 正如在其他問題中所建議的,我甚至嘗試過使用不同的方法來調用部分,這兩種方法都使用
render(:partial => 'calendars/analysis')
和(render 'calendars/analysis')
。
但是這些解決方案都沒有奏效。
-----
我已經做了不少關於這一課題的研究,我不得不說,我現在很困惑。
雖然上面提到的兩個教程都推薦了這種方法,但其他資源including this one指出,您無法在Rails中渲染部分資產。
因此:
- 什麼是錯我當前的代碼?
- 我嘗試使用的方法是否有意義,如果不是,我該做什麼?
你的'analysis.js.erb'是否存在於'views'目錄下的某處? –
感謝您的評論。 'analysis.js.erb'目前在'app/assets/javascript'下。這會導致問題嗎? –