【问题标题】:getting id of parent element in rails rjs在rails rjs中获取父元素的ID
【发布时间】:2010-06-23 02:45:02
【问题描述】:

我正在使用一个允许用户将新内容添加到装运箱的产品。例如:

用户正在设置一个货件,每个货件可以包含多个箱子,每个箱子可以包含多个内容。

所以我有link_to_remote 像这样连接:

在我的部分货箱中:

<div class="shipping_box" id="shipping_box">
    #some code
    <%= link_to_remote "Add box conents", :url => {:action=> 'add_box_contents'} %>
</div>

add_box_contents.rjs:

page.insert_html :bottom, "shipping_box", :partial => 'box_content', :object => BoxContent.new

_box_content.erb

<div class="box_contents" id="box_contents">
   box contents partial rendered
</div>

对于我的第一个运输箱,一切正常,但是当动态添加第二个运输箱时,_box_content.erb 部分始终呈现在第一个箱的 &lt;div&gt; 中。当然这是因为 id 被指定为shipping_box,并且所有盒子共享这个 id。那么我的问题是,如何在 div 中为正确的包含框部分呈现新的框内容,而不仅仅是删除第一个框?

此屏幕截图显示了第一个框,其中包含 2 条动态添加的内容行(下拉菜单)。我希望第二个框的“添加订单行项目”行向我的第二个框的内容添加一个下拉列表。 alt text http://img12.imageshack.us/img12/6274/screenshot20100622at114.png

【问题讨论】:

  • 出于好奇,您使用的是 jRails 插件吗? (希望如此;那么我们可以使用 jQuery)
  • 目前没有,但如果这是最干净的方式,我可以。你有什么建议?

标签: ruby-on-rails rjs erb


【解决方案1】:

您的 rjs 可能应该使用 replace_html() 而不是 insert_html()

page.replace_html "shipping_box", :partial => 'box_content', :object => BoxContent.new

【讨论】:

  • 我正在尝试添加新框内容的多个实例。我添加了一个屏幕截图以进行澄清。
【解决方案2】:

您实际上想要渲染部分“select_order_line”,以便仅将相应的部分添加到运输箱中。所以把它分解出来,然后你可以写:

page.insert_html :bottom, "box_content", :partial => 'select_order_line'

【讨论】:

    猜你喜欢
    • 2012-05-03
    • 2012-04-12
    • 2017-12-18
    • 2011-06-07
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多