在 Ruby on Rails 2.3.x 中向 GET 请求发送参数
我是 Rails 新手,目前使用的是 2.3.8。我正在为 iOS 应用程序设计 Web 服务。
由于我通常是一名客户端 iPhone 开发人员,因此我对服务器端实现的了解非常少。
我遇到的问题是我试图将 GET 请求中的一些参数传递给我的控制器之一中的索引操作。这是使用默认值进行路由的,
map.resources :dishes
其中菜肴是使用脚手架创建的控制器的名称。
我想将多个参数传递给新操作或索引,这将允许我过滤要返回的菜肴对象。
索引方法看起来像
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 服务开发的新手,所以如果您有任何建议,我会欢迎他们。
谢谢。
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/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"}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,你的
不是 Ruby :) 有三种方法可以创建
Array
实例,还要注意
它告诉你错误发生在
new
中,而不是中索引
You have
which is not Ruby unfortunately :) There are three ways to create an instance of
Array
also pay attention to
which tells you that error happened in
new
not inindex