在Ruby on Rails 2.3.x中向GET请求发送参数

时间:2011-10-07 09:49:08

标签: ruby-on-rails api get params

我是rails的新手,我目前正在使用2.3.8。我正在为iOS应用程序设计一个Web服务。

由于我通常是客户端的iPhone开发人员,因此我对服务器端实现的了解极少。

我遇到的问题是我正在尝试将GET请求中的一些参数传递给我的一个控制器中的索引操作。这是使用默认

路由的
map.resources :dishes

其中,dish是使用脚手架创建的控制器的名称。

我想将多个参数传递给新动作或索引,这将允许我过滤菜肴对象以返回。

索引方法类似于

def index
    if(params[:latitude])
     @min = 0.5 - 1
      @max = 0.5 + 1
      @locations = Location.find(:all, :conditions => [":latitude > ? AND :latitude < ?", @min, @max])
        @dishes = new Array
        @locations.each do |loc| 

          @dishes = @dishes + loc.dishes

        end

        respond_to do |format|
          format.html # index.html.erb
          format.json {render :json => @dishes.to_json(:except => [ :user_id, :location_id , :like_id], :include => { :user => { :only => [:id, :name] } }  ) }

        end
    else
      @dishes = Dish.all

      respond_to do |format|
        format.html # index.html.erb
        format.json {render :json => @dishes.to_json(:except => [ :user_id, :location_id , :like_id], :include => { :user => { :only => [:id, :name] } }  ) }

      end
    end
end

当我传入额外的参数时,我目前收到以下错误

ArgumentError in DishesController#index
wrong number of arguments (1 for 0)

完整错误:

ArgumentError in DishesController#index
wrong number of arguments (1 for 0)
RAILS_ROOT: /Users/jon/Documents/Dev/Research/FF5_Rails_Test/FF5

Application Trace | Framework Trace | Full Trace
/Users/jon/Documents/Dev/Research/FF5_Rails_Test/FF5/app/controllers/dishes_controller.rb:9:in `new'
/Users/jon/Documents/Dev/Research/FF5_Rails_Test/FF5/app/controllers/dishes_controller.rb:9:in `index'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `perform_action_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in `call_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/activesupport-    2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/flash.rb:151:in `perform_action'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `process_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:606:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:391:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:386:in `call'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:438:in `call'
Request

Parameters:

{"format"=>"json",
 "latitude"=>"0.5"}
Show session dump

Response

Headers:

{"Content-Type"=>"",
 "Cache-Control"=>"no-cache"}

我意识到这与Rails的RESTful设计略有不同,但我无法通过另一种方式来实现它。

同样,我是rails / web服务开发人员的新手,所以如果你有任何提示,我会欢迎他们。

感谢。

1 个答案:

答案 0 :(得分:2)

你有

@dishes = new Array
不幸的是,

不是Ruby :)有三种方法可以创建Array

的实例
@dishes = Array.new
@dishes = []
@dishes = Array( nil )

也要注意

/Users/jon/Documents/Dev/Research/FF5_Rails_Test/FF5/app/controllers/dishes_controller.rb:9:in 'new'
/Users/jon/Documents/Dev/Research/FF5_Rails_Test/FF5/app/controllers/dishes_controller.rb:9:in 'index'

告诉您new中的错误发生在index