简体   繁体   中英

Rails use ruby in link_to

Instead of hard coding a link description, I would like to use some Ruby code.

This is the original:

<li><%= link_to "Open Projects List", workorders_index2_path %></li>

This didn't work for me:

<li><%= link_to "<%= current_tenant.name_workorder.capitalize.pluralize %>", workorders_index2_path %></li>

Thanks for the help!

您根本不需要使用引号:

<li><%= link_to current_tenant.name_workorder.capitalize.pluralize, workorders_index2_path %></li>

You already ARE using ruby code. <%= %> everything inside of that is pure ruby. link_to is a ruby method, and "Open Projects List" is the first parameter to that method, it's a string. Anything you can do in ruby you can send here - don't send a string, send the variable:

<li><%= link_to current_tenant.name_workorder.to_s.capitalize.pluralize, workorders_index2_path %></li>

You can also use string interpolation like you would with regular ruby:

<li><%= link_to "Open Project #{current_tenant.name_workorder}", workorders_index2_path %></li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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