开发者

Rails nth partial condition

开发者 https://www.devze.com 2022-12-23 18:21 出处:网络
I noticed the d开发者_C百科ocs have: <%= item_counter %> But I have a partial which I want to add a class into on every 6th one, is there an easy way to do this?You can pass variables into th

I noticed the d开发者_C百科ocs have:

<%= item_counter %>

But I have a partial which I want to add a class into on every 6th one, is there an easy way to do this?


You can pass variables into the partial's instance scope when you call it, and inside the partial you can then take action based on the value of those instance variables.

So if you have the local variable item_counter in the view containing the partial, render the partial like this:

 <%= render :partial => "my_partial", :locals => { :item_counter => item_counter} %>

Inside the partial, you will now be able to access the local variable item_counter. Thus inside the partial you could have:

if item_counter > 0 && item_counter % 6 == 0
    # Do exciting base 6 activity
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号