【问题标题】:Rails - is it possible to use ruby code in RJS files?Rails - 是否可以在 RJS 文件中使用 ruby​​ 代码?
【发布时间】:2010-07-10 06:51:23
【问题描述】:

是否可以在 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 调用中,RJS 文件用于响应,它会根据调用销毁操作的模板给出不同的响应。

此时,进行了 AJAX 调用,记录被销毁,然后什么也没有发生,无论哪个模板调用了销毁操作。 所以我想知道它是否不工作仅仅是因为我不能在 RJS 文件中使用 ruby​​ 代码。有任何想法吗?还是我完全做错了?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ajax rjs


    【解决方案1】:

    对,我重做了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 文件,并根据传入的模板名称使用不同的文件。 耶!

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 2011-02-09
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多