Monday, May 21, 2012

rails - how to make a GET request to action with parameters

I'm hoping this problem is a fairly simple one as I am relatively new to rails development.
I am trying to make a get request from a controller with a specified action and pass required parameters. This is the relevant code in the helper class:



module ChartsHelper
def chart_tag (action, height, params = {})
params[:format] ||= :json
path = charts_path(action, params)
content_tag(:div, :'data-chart' => path, :style => "height: #{height}px;") do
image_tag('loading.gif', :size => '32x32', :class => 'spinner')
end
end
end


and the corresponding action in the ChartsController:



class ChartsController < ApplicationController

def week_events_bar_chart
days = (params[:days] || 30).to_i
render :json => {
:type => 'AreaChart',
:cols => [['string', 'Date'], ['number', 'subscriptions']],
:rows => (1..days).to_a.inject([]) do |memo, i|
date = i.days.ago.to_date
t0, t1 = date.beginning_of_day, date.end_of_day
subscriptions = Kpsevent.all.count
memo << [date, subscriptions]
memo
end.reverse,
:options => {
:chartArea => { :width => '90%', :height => '75%' },
:hAxis => { :showTextEvery => 30 },
:legend => 'bottom',
}
}
end
end


The routes file has the following:



resource :charts do
get 'week_events_bar_chart'
end


However I get the following output when trying to perform this request:



 Started GET "/charts.week_events_bar_chart?days=14" for 127.0.0.1 at Tue May 22 00:31:48 +1200 2012
Processing by ChartsController#index as
Parameters: {"days"=>"14"}


And the controller action is never called.
Is anyone able to help with this problem?



EDIT: rake routes output:



week_events_bar_chart_charts GET    /charts/week_events_bar_chart(.:format) {:controller=>"charts", :action=>"week_events_bar_chart"}
POST /charts(.:format) {:controller=>"charts", :action=>"create"}
new_charts GET /charts/new(.:format) {:controller=>"charts", :action=>"new"}
edit_charts GET /charts/edit(.:format) {:controller=>"charts", :action=>"edit"}
GET /charts(.:format) {:controller=>"charts", :action=>"show"}
PUT /charts(.:format) {:controller=>"charts", :action=>"update"}
DELETE /charts(.:format) {:controller=>"charts", :action=>"destroy"}




No comments:

Post a Comment