简体   繁体   English

在Rails的link_to内设置文本的ID

[英]Set id for text inside link_to in rails

I have a dropdown list having links to specific categories, in each line of category I want it to include name of category and number of books of that category. 我有一个下拉列表,具有指向特定类别的链接,在类别的每一行中,我希望它包括类别名称和该类别的书籍数量。 Anyway, I want the number part having a different style in comparison with name part. 无论如何,我希望数字部分与名称部分具有不同的样式。

Here is the code for each one: 这是每个代码:

%li= link_to "#{category.name} (#{category.books.count})" , root_path(category_id: category.id)      

I want to set id for #{category.name} and (#{category.books.count}) , what should I do? 我想为#{category.name}(#{category.books.count})设置ID,该怎么办?

You should define the attribute id . 您应该定义属性id In your code, you should do it like: 在您的代码中,您应该这样做:

%li= link_to "#{category.name} (#{category.books.count})" , root_path(category_id: category.id), id: "#{category.name} (#{category.books.count})"

I found the answer, we should use link_to do like this: 我找到了答案,我们应该使用link_这样做:

 = link_to root_path(category_id: category.id, per_page: params[:per_page]) do
          .row.cate-display
            #cate-name= category.name
            #cate-count= "(#{category.books.count})"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM