2013-08-06 22 views
0

如果存在,我需要爲所有鏈接添加參數sortRails向所有鏈接添加排序參數的最佳方式?

解決方案我想到:

  1. 使用的link_to ..不方便,我認爲這是醜陋的。

    link_to 'Something', tag_url(tags, params.except(:controller, :action))

  2. 創建自定義鏈接幫手

  3. Owerwrite的link_to助手(試過沒有成功)

什麼是最好的做法?

更新:

def link_to_with_params(*args, &block) 
    if block_given? 
     options  = args.first || {} 
     html_options = args.second 
     link_to(capture(&block), options, html_options) 
    else 
     name   = args[0] 
     options  = args[1] || {} 
     html_options = args[2] 

     html_options = convert_options_to_data_attributes(options, html_options) 
     url = url_for(options) # Add params.except(:controller, :action) 

     href = html_options['href'] 
     tag_options = tag_options(html_options) 

     href_attr = "href=\"#{ERB::Util.html_escape(url))}\"" unless href 
     "<a #{href_attr}#{tag_options}>#{ERB::Util.html_escape(name || url)}</a>".html_safe 
    end 
    end 

我如何通過params.except(:controller, :action)的URL在上面的幫手?

+0

爲什麼downvote ..? –

+0

我認爲第二個選擇要好得多 – Salil

+0

@Salil - 好的,我已經複製link_to助手方法並重命名它,我如何在助手方法中添加參數?我更新了我的問題。 –

回答

1

嘗試以下

def link_to_with_params(params, name, options = {}, html_options = {}) 
    options.merge!(params.except(:controller, :action)) if params && params.respond_to?(:merge!) 
    link_to(name, options, html_options) 
end 
相關問題