Rails - 是否可以在 RJS 文件中使用 ruby​​ 代码?

发布于 2024-09-09 20:45:27 字数 919 浏览 4 评论 0原文

是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2024-09-16 20:45:27

是的,我重新编写了 destroy 函数,使其看起来像这样:

  def destroy
    #is this bad because its not linked to current_user?
    #how would you even set it to be relational to current_user? 
    @viewer = Viewer.find(params[:id])
    @viewer_registry = Registry.find(@viewer.registry_id)
    @viewer.destroy
    flash[:notice] = "Viewer deleted"
    respond_to do |format|
      format.html { redirect_to registry_path(@viewer_registry) }
      format.js {
        if params[:template] == "viewer"
          render :action => "viewer_destroy.js.rjs"
        else
          render :action => "destroy.js.rjs"
        end
      }
    end
  end

所以我有 2 个 RJS 文件,并且根据传入的模板名称使用不同的文件。
耶!

Right, I redid the destroy function so that it looked like this:

  def destroy
    #is this bad because its not linked to current_user?
    #how would you even set it to be relational to current_user? 
    @viewer = Viewer.find(params[:id])
    @viewer_registry = Registry.find(@viewer.registry_id)
    @viewer.destroy
    flash[:notice] = "Viewer deleted"
    respond_to do |format|
      format.html { redirect_to registry_path(@viewer_registry) }
      format.js {
        if params[:template] == "viewer"
          render :action => "viewer_destroy.js.rjs"
        else
          render :action => "destroy.js.rjs"
        end
      }
    end
  end

So I've got 2 RJS files instead, and different ones are used based on the incoming template name.
Yay!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文