Use a partial in a partial?

I’m a Rails newbie, so bear with me.

I have a few places, some pages, some partials that use:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><%= link_to "delete", post, method: :delete,
data: { confirm: "You sure?" },
title: post.content %>
</code>
<code><%= link_to "delete", post, method: :delete, data: { confirm: "You sure?" }, title: post.content %> </code>
<%= link_to "delete", post, method: :delete,
                                 data: { confirm: "You sure?" },
                                 title: post.content %>

Would it make sense to make this a partial since it is used repeatedly, sometimes in other partials too?

Is it o.k. to put partials in partials?

I would try and limit the use of partials within other partials, since it can negatively affect view rendering speed (i.e. rendering lots of partials is super slow), but that said, I have to say, yes it is okay to use partials within other partials.

A couple things you should probably realize about this:

  1. It can get confusing as all hell, especially when:
    • you’re passing around a lot of local variables.
    • you’re rendering collections, i.e. looping over a partial with an array of some sort.
  2. Inferring from the fact that you’re asking this question, you probably don’t know that you’re already rendering partials within partials.
    • what I mean by this, is that the layout (presumably “application.html.erb”) is really going to be your only non-partial in a view.
    • Whenever you have an action rendered by a view, i.e. ‘show’, ‘new’, ‘edit’ (assuming you’re rest-ful), you should realize that the corresponding view (i.e. ‘show.html.erb’ for example) is a partial being rendered within the main layout file.
    • Therefore any use of a partial that you yourself make (‘_users.html.erb’ or what have you) is actually already a ‘partial within another partial’.

Kinda feels like Inception, doesn’t it? 🙂


As for your specific example, I agree with the other answerer that you should probably just use it as a helper. And you can use it across many models so long as you abstract enough of the stuff away in the helper, like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def delete_link_for(polymorphic_object, method_symbol)
link_to "delete", polymorphic_object, method: :delete,
data: { confirm: "You sure?" },
title: post.send(method_symbol)
end
</code>
<code>def delete_link_for(polymorphic_object, method_symbol) link_to "delete", polymorphic_object, method: :delete, data: { confirm: "You sure?" }, title: post.send(method_symbol) end </code>
def delete_link_for(polymorphic_object, method_symbol)
   link_to "delete", polymorphic_object, method: :delete,
                             data: { confirm: "You sure?" },
                             title: post.send(method_symbol)
end

That way you can invoke like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <%= delete_link_for(@post, :content) %>
<%= delete_link_for(@user, :username) %>
</code>
<code> <%= delete_link_for(@post, :content) %> <%= delete_link_for(@user, :username) %> </code>
 <%= delete_link_for(@post, :content) %>
 <%= delete_link_for(@user, :username) %>

etc.

I think a template is overkill for such a small smippet. I would write a helper method instead.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật