使用haml中的link_to传递参数

时间:2013-02-15 20:30:03

标签: ruby-on-rails

%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,例如:。

1 个答案:

答案 0 :(得分:12)

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

= link_to account.id, account

您始终可以在路径中明确添加任何其他参数:

= link_to account.id, account_path(:id => account.id, :foo => "bar")