开发者

Sending params to GET request in Ruby on Rails 2.3.x

开发者 https://www.devze.com 2023-04-11 15:27 出处:网络
I\'m new to rails and I\'m currently using 2.3.8. I\'m designing a web service for an iOS app. As I\'m normally a client side iPhone developer, my knowledge of server side implementation is shamefull

I'm new to rails and I'm currently using 2.3.8. I'm designing a web service for an iOS app.

As I'm normally a client side iPhone developer, my knowledge of server side implementation is shamefully minimal.

The problem I'm having is that I'm trying to pass some params in a GET request to the index action in one of my controllers. This is routed using the default

map.resources :dishes

where dishes is the name of the controller, created with scaffold.

I would like to pass multiple params to either a new action or to the index, which will allow me to filter the dish objects to return.

The index method looks like

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

I am currently getting the following error when I pass in an extra param

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

The full error:

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/actionpa开发者_Go百科ck-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"}

I realise this goes slightly against the RESTful design of Rails but I couldn't work out another way to do it.

Again, I'm a new to rails / web service dev so if you have any tips I would welcome them.

Thanks.


You have

@dishes = new Array

which is not Ruby unfortunately :) There are three ways to create an instance of Array

@dishes = Array.new
@dishes = []
@dishes = Array( nil )

also pay attention to

/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'

which tells you that error happened in new not in index

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号