首页 文章

获取rails rjs中父元素的id

提问于
浏览
0

我正在使用允许用户将新内容添加到货件箱的a . 例如:

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

所以我有 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 partial总是在第一个框的 <div> 中呈现 . 当然这是因为id被指定为 shipping_box ,并且所有框都共享此id . 那么我的问题是,如何在div中为正确的包含盒子部分渲染新的盒子内容,而不仅仅是第一个盒子?

此屏幕截图显示第一个框,其中包含2个动态添加的内容行(下拉列表) . 我希望第二个框的"add order line item"行在我的第二个框的内容中添加一个下拉列表 . alt text http://img12.imageshack.us/img12/6274/screenshot20100622at114.png

2 回答

  • 0

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

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

    您实际上想要渲染部分“select_order_line”,以便只将相应的部件添加到装运箱中 . 因此,只需考虑它,然后你可以写:

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

相关问题