3
%tbody
  - @accounts.each do |account|
    %tr
      %td= link_to account['id'],show_path,{:id => account['id']}
      %td= account['name']
      %td= account['description']
      %td= account['created']

以上只是haml文件的一个片段,在我的控制器中我有以下内容:

def show 
  # If a system account already exists in session, it was found from a search with the account id
  # Otherwise, this is a new search for a system account by the given id
  @account = session[:account]
  if @account.nil?
    Rails.logger.debug { "Querying for the account with id: #{params[:id]}" }
    response = query_account(CGI.escape(params[:id]))
    @account = JSON.parse(response.body)
  end
end

路线(show_path)是 /system_accounts/:id

如果 id 为 23,我如何将参数 id 传递给控制器​​并链接 /system_accounts/23 例如:。

4

1 回答 1

12

这将链接到该帐户的正确显示页面:

= link_to account.id, account

您始终可以向路径显式添加任何其他参数:

= link_to account.id, account_path(:id => account.id, :foo => "bar")
于 2013-02-15T21:01:53.723 回答