2013-12-19 61 views
3

我已经在这个库中看到这个代码:的link_to与Rails的块

<%= link_to(:controller => 'account', :action => 'select', :account_id => account.customer_id) { account.customer_id.to_s } %> 
    (<%= account.login %>/<%= account.company_name %>) 

这实际上转换为下面的HTML:

<a account_id="8282277272" action="select" controller="account">8282277272</a> 
     (loginname/companyname) 

我想知道你将如何传递到的link_to块为了使这项工作?

+0

我不关注。为什么会需要一个块。您发布的代码不使用或不需要块。 – cpjolicoeur

+0

您可以看到{account.customer_id.to_s}被传递给link_to。我不确定为什么开发者会这样做,但我知道一个块可以通过。 –

+0

尝试使用url_for:link_to(url_for(:controller =>'account',:action =>'select',:account_id => account.customer_id)){account.customer_id.to_s}' – MrYoshiji

回答

2

我认为这是你在找什么。 “do..end”内部的内容将被放置在标签内。

<%= link_to(:controller => 'account', :action => 'select', :account_id => account.customer_id) do %> 
    (<%= account.login %>/<%= account.company_name %>) 
<% end %> 

应该产生

<a href="<path to controller with account_id parameter>"> 
    (username/Company, Inc.) 
</a> 

什么在你原来的代码发生是表达{ account.customer_id.to_s }正在为块的link_to通过。如果要将客户ID与“登录”和“公司名称”一起显示,请将其放入块中。