【问题标题】:ArgumentError - wrong number of arguments (given 2, expected 0..1): sorting issue Rails 4.2ArgumentError - 参数数量错误(给定 2,预期 0..1):排序问题 Rails 4.2
【发布时间】:2016-07-11 16:59:28
【问题描述】:

我正面临这样的问题:

ArgumentError - 参数数量错误(给定 2,预期 0..1):
actionpack (4.2.6) lib/action_dispatch/routing/url_for.rb:156:in url_for' actionview (4.2.6) lib/action_view/routing_url_for.rb:94:inurl_for' actionview (4.2.6) lib/action_view/helpers/url_helper.rb:181:in link_to'
app/helpers/application_helper.rb:7:in
sortable'
app/views/contacts/_sort.html.erb:3:in `_app_views_contacts__sort_html_erb

这是我的 application_helper.rb:

  def sortable(column, title = nil)
    title ||= column.titleize.downcase
    css_class = column == sort_column ? "current #{sort_direction}" : nil
    direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
    link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
  end

在上面的代码中,link_to 是我在日志中收到错误的行。 这是我的 _sort.html.erb(从索引文件渲染):

<div id="sort_control"><a href="#">sort</a></div>
<ul class="sort" style="display:none;">
    <li><%= sortable "created_at" %></li> #here I am getting error
    <li><%= sortable "firstname" %></li>
  <li><%= sortable "lastname" %></li>
  <li><%= sortable "email" %></li>
</ul>

这是我的控制器方法:

def index
    @contacts = Contact.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 60, :page => params[:page])
end

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    错误表示您将 2 个参数传递给 url_for 方法的 link_to 部分,而它只需要 1 个。

    你试过了吗?:

    link_to title, {params.merge(sort: column, direction: direction, page: nil)}, class: css_class
    

    【讨论】:

    • 出现错误:syntax error, unexpected '}', expecting =&gt; ...rection: direction, page: nil)}, class: css_class ...
    • 您如何在您的application_helper.rb 文件中获得sort_directionsort_column
    【解决方案2】:

    我对 Rails 还很陌生,但它看起来不喜欢你的类被包裹在 {}

    link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
    

    根据docs,应该是这样的:

        link_to title, params.merge(:sort => column, :direction => direction, :page => nil), class: css_class
    

    示例:

    link_to "Articles",articles_path,id:"news",class:"article"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多