Ruby on rails 在.erb文件中使用link_to,连接语法
在index.html.erb中,我希望每个学院都有如下链接:Apply-$25Ruby on rails 在.erb文件中使用link_to,连接语法,ruby-on-rails,concatenation,erb,Ruby On Rails,Concatenation,Erb,在index.html.erb中,我希望每个学院都有如下链接:Apply-$25 我希望显示的价格根据college.Undergrade\u app\u费用的价值进行更改 这是我尝试过的,但不起作用。也许有一种特殊的连接语法可以将显式的“Apply-”与price结合起来,或者我需要以某种方式进行转义,或者有一种特殊的方式使用link\u to来实现这一点 <td><%= link_to 'Apply - ' college.undergrad_app_fee, col
<td><%= link_to 'Apply - ' college.undergrad_app_fee, college.application_url %></td>
使用字符串插值语法:
<td><%= link_to "Apply - #{college.undergrad_app_fee}", college.application_url %></td>
跟进:
对于条件链接,使用or,它们应该相对简单易用
处理货币格式化的nil
案例有点棘手。您可以使用|
操作符来执行此操作
将这两种方法结合起来,可以得出以下结论:
<td><%= link_to_if college.application_url, "Apply - #{number_to_currency(college.undergrad_app_fee || 0)}", college.application_url %></td>
哇,回答得很好-而且速度也很快!谢谢我会把你的答案标为正确答案。如果你愿意,我还有一个后续问题:如果college.Undergrade\u app\u费用为空,我希望它显示0美元。如果college.application\u url为空,我不希望它是链接。有什么想法吗?
<td><%= link_to_if college.application_url, "Apply - #{number_to_currency(college.undergrad_app_fee || 0)}", college.application_url %></td>
> helper.number_to_currency(12)
=> "12,00 €"
> nil || 0
=> 0
> 12 || 0
=> 12