Rails 4:escape_javascript渲染部分不起作用 [英] Rails 4: escape_javascript render partial not working
问题描述
在我的Rails 4应用程序中,我试图将某些视图显示为modals
(具有Bootstrap模式),而不是常规的.html.erb
视图.
In my Rails 4 app, I am trying to display some views as modals
(with Bootstrap modal), instead of the regular .html.erb
views.
例如,我有一个calendar
模型,在控制器中有一个自定义的analysis
动作:
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()
})
最后但并非最不重要的一点是,我使用<%= render 'analysis' %>
助手和以下链接,从呈现在日历show.html.erb
视图内的_heading.html.erb
部分调用了模态:
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:
- 按照此处的建议,我同时尝试了
escape_javascript render
和. - 根据那里的建议,我也尝试了
render_to_string
而不是只是render
. - 按照另一个问题的建议,我什至尝试了使用
render(:partial => 'calendars/analysis')
和(render 'calendars/analysis')
的不同方式来调用部分.
- 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.
虽然上述两个教程都推荐了这种方法,但其他资源包括,请指出您不能从Rails中的资产渲染部分.
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.
因此:
- 我当前的代码有什么问题?
- 我尝试使用的方法是否完全有意义,如果没有,我应该怎么做?
推荐答案
app/assets
下的代码无法调用render
.实际上,除了很少的方法外,它几乎不能调用任何东西.
The code under app/assets
can not call render
. Actually it can not call almost anything except a very few methods.
将其移动到app/views/somethings/analysis.js.erb
其中somethings
是您的控制器名称的复数形式,因此只需将其放在_analysis.html.erb
附近.
Where somethings
is the name of your controller in plural form, so just put it near the _analysis.html.erb
.
这篇关于Rails 4:escape_javascript渲染部分不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!