passing get parameters through named route and then to controller model; stringify_keys!
Hey, I'm just learning ruby on rails and I've been stumped on this for awhile now.
Here's my url request:
http://192.168.2.20:8080/Location/new/123.123,-123.123/
Here's my routes.rb:
map.connect '/Location/new/:coords/', :controller => 'Location', :action => 'new', :coords => /\d+.\d+,-\d+.\d+/
map.connect '/Location/list/', :controller => 'Location', :action => 'list'
map.connect '/Location/create/', :controller => 'Location', :action =&g开发者_如何学Got; 'create'
Here's my location_controller.rb
def new
@coords = Location.new(params[:coords])
end
Here's the error message it gives me:
NoMethodError in LocationController#new
undefined method `stringify_keys!' for "123.123,-123.123":String
Location.new expects a Hash as its argument, in location_controller.rb you should use:
def new
@location = Location.new( { :coords => params[:coords] } )
end
Assuming the coords is the name of the field you want to use. Then in your view or subsequent code you can use @location.coords
to get the value.
精彩评论