在Ruby on Rails上获取“错误数量的参数”

时间:2018-08-29 07:47:01

标签: ruby-on-rails ruby-on-rails-5.2

错误的参数数量(给定2个,预期为1个)

SportsController

class SportsController < ApplicationController
  def index
    @sport = Sport.all
    @events, @errors = Bapi::Inplay.all(query)
  end

  private
    def query
      params[:query, {}]
    end
end

体育index.html.erb

 <% @sports.each do |sport| %>
   <% @events(:sport_id => sport.id).each_slice(2) do |events| %>

我想将每个sport.id发送到@enevts实例变量


编辑: 当在SportsController中将查询作为哈希发送时!

class SportsController < ApplicationController
  def index
    @sport = Sport.all
    query = {:sport_id => 1} 
    @events, @errors = Bapi::Inplay.all(query)
  end

  private
    def query
      params[:query, {}]
    end
end

Index.html.erb

 <% @sports.each do |sport| %>
   <% @events.each_slice(2) do |events| %>

1 个答案:

答案 0 :(得分:4)

params是一个哈希,方法:[]只能接受1个参数。

 def query
    params[:query] || {}  # Will return :query part or empty Hash if it has nothing
 end