1

destroy为特定资源定义了一个操作。完成后,它使用具有视觉效果的 rjs 文件从调用它的页面中删除资源。

destroy我的问题是可以从 2 个不同的模板调用该操作。所以视觉效果需要不同,这取决于它是从哪个模板调用的......有没有办法这样做......?

1:我认为我可以做到这一点的一种方法是有一个destroy动作和一个destroy-variant动作,一个模板调用该destroy-variant动作,另一个调用普通模板......但是,我很确定这是反对的干的想法。老实说,我也不完全确定该怎么做……:method => :delete, :action => :destroy-variant在 URL 选项中使用会起作用吗?

2:另一种对我来说似乎可行的方法是找出哪个页面正在调用该操作,然后酌情推出不同的 rjs 文件。但我完全不知道如何区分哪个页面正在执行操作调用。

3:最后的方法只是让我重做模板/RJS,以便可以将相同的视觉效果应用于两个模板。

我会很感激你的建议!:)

4

1 回答 1

2

您没有提及您正在使用的 javascript 库。这个解决方案是基于 jquery 的。只需一个动作就可以很容易地做你想做的事destroy。但我会建议一种不同的技术来解决它。我发现 rjs 很受限制。去 jquery taconite 插件。它的所有标记,而不是......一堆与 erb 混合的 javascript 不太吸引人的眼睛!

因此,现在destroy.rjs您将拥有一个destroy.xml.erb. 在我进入您需要添加的内容之前,让我们看看您需要为您的destroy操作进行哪些修改。

前:

   def destroy
        @model = Model.find(params[:id])
        @model.destroy

          respond_to do |format|
            format.html { redirect_to :action => :index }
            format.xml
          end
    end

后:

   def destroy
        @model = Model.find(params[:id])
        @model.destroy
        @template = params[:template]
          respond_to do |format|
            format.html { redirect_to :action => :index }
            format.xml
          end
    end

在你的 routes.rb 中添加一个命名路由来简化事情:

map.destroy_using_template 'destroy/:id/:template', :controller => "controller", :action => "destroy"

在您看来:

将 jquery taconite 插件添加到您的标题中<layout>.html.erb

<%= javascript_include_tag "jquery.js", "jquery.taconite.js" %>

在模板 1 的视图中:

假设您使用链接删除。

   <%= link_to "Delete", destroy_using_template_path(:id => "1", :template => "temp1"), :method => :delete, :confirm => "Are you sure you want to delete the record?" %>

和模板 2 相同:

假设您使用链接删除。

   <%= link_to "Delete", destroy_using_template_path(:id => "1", :template => "temp2"), :method => :delete, :confirm => "Are you sure you want to delete the record?" %>

现在在你的destroy.xml.erb

<taconite>
    <% if @template=="temp1" %>
      <remove select="#template1" />
    <% elsif @template=="temp2" %>
      <remove select="#template2" />
    <% end %>

    <eval>
        //OPTIONAL: Execute some javascript here if you want. You can do most of the DOM modifications using taconite itself.
        alert("HEY!!!");
    </eval>
</taconite>

现在不容易吗?:)

请记住,在使用 taconite 时,您必须确保 destroy.xml.erb 中的代码符合 XML。确保关闭所有打开的标签。在此处阅读有关铁燧石的更多信息:http: //malsup.com/jquery/taconite/

Taconite 在标记中实现了所有常规的 jquery DOM 修饰符以及它自己的一些额外修饰符。如果您想切换到 javascript,只需在eval标签中包含该 javascript,即可轻松完成,如上所示。

于 2010-07-04T15:02:01.807 回答