调用format.js时,获取的参数太少会出错

时间:2017-01-20 17:48:50

标签: ruby-on-rails format render ruby-on-rails-5

我正在使用Rails 5.我有这种控制器方法

  def import_matching_objects
    matching_objects = objectTime.find_by_name_and_hometown_and_age(current_user)
    matching_objects.each do |object_time|
      user_object_time_match = UserObjectTimeMatch.new(:user_id => user.id,
                                                 :object_time_id => object_time.id,
                                                 :matches => true)
      # Save objects
      if !user_object_time_match.save_and_create_object_time
        puts "Failed to save user object: #{user_object_time_match.errors.full_messages}" 
      end 
    end 
    format.js { render js: "window.location='/objects/index'" } 
  end

但它产生了这个错误

ArgumentError (too few arguments):

app/controllers/user_object_time_matches_controller.rb:31:in `format'

在“format.js {render js:”window.location ='/ objects / index'“}”行上。我的语法似乎与我在网上找到的每个例子一致。我错过了什么?

1 个答案:

答案 0 :(得分:0)

respond_to阻止应解决ArgumentError (too few arguments)问题:

respond_to do |format|
    format.js { render js: "window.location='/objects/index'" }   
end