Rails - 是否可以在 RJS 文件中使用 ruby 代码?
是否可以在 RJS 文件中使用 ruby 代码?
例如,destroy.js.rjs 文件
if @template == "viewer"
page["viewing_registry_#{@viewer_registry.id}"].replace_html :partial => "shared/request_viewer_link"
else
page["viewer_#{@viewer.id}"].visual_effect :DropOut, :duration => 2.0
flash.discard
end
这是从销毁操作调用的 RJS 文件,该文件具有
def destroy
@viewer = Viewer.find(params[:id])
@viewer_registry = Registry.find(@viewer.registry_id)
@viewer.destroy
flash[:notice] = "Viewer deleted"
@template = params[:template]
params[:template] = nil
respond_to do |format|
format.html { redirect_to registry_path(@viewer_registry) }
format.js
end
end
来自 AJAX 调用的 So,RJS 文件用于响应,并且它给出了不同的响应基于调用销毁操作的模板的响应。
此时,进行 AJAX 调用,记录被销毁,然后无论哪个模板调用销毁操作,都不会发生任何事情。 所以我想知道它是否不起作用只是因为我无法在 RJS 文件中使用 ruby 代码。有什么想法吗?或者我完全做错了?
谢谢!
Is it possible to use ruby code in RJS files?
For example, the destroy.js.rjs
file
if @template == "viewer"
page["viewing_registry_#{@viewer_registry.id}"].replace_html :partial => "shared/request_viewer_link"
else
page["viewer_#{@viewer.id}"].visual_effect :DropOut, :duration => 2.0
flash.discard
end
This is the RJS file called from a destroy action that has a
def destroy
@viewer = Viewer.find(params[:id])
@viewer_registry = Registry.find(@viewer.registry_id)
@viewer.destroy
flash[:notice] = "Viewer deleted"
@template = params[:template]
params[:template] = nil
respond_to do |format|
format.html { redirect_to registry_path(@viewer_registry) }
format.js
end
end
So from an AJAX call, the RJS file is used for the response, and it gives a different response based on which template is calling the destroy action.
At the moment, the AJAX call is made, the record is destroyed, and then nothing happens, regardless of which template was calling for the destroy action.
So I'm wondering whether it is not working simply because I can't use ruby code in an RJS file. Any ideas? Or am I doing it wrong entirely?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我重新编写了 destroy 函数,使其看起来像这样:
所以我有 2 个 RJS 文件,并且根据传入的模板名称使用不同的文件。
耶!
Right, I redid the destroy function so that it looked like this:
So I've got 2 RJS files instead, and different ones are used based on the incoming template name.
Yay!