0

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"}
jmc
  • 620
  • 14
  • 24

2 Answers2

1

To your comment on console:

in rails chart_path(x,...) will generate a route to ChartsController#show with param id = x, which is by default GET /charts/x. By naming your parameter 'action', you fooled yourself, 'week_events_bar_chart' will just be the id in the restful route.

To your original code: charts_path(x, params) will route to ChartsController#index with format x, which looks like GET /charts.week_events_bar_chart, again calling it action fooled you.

What you need is the named route helper week_events_bar_chart_charts_path for your action.

But since you seem to want your helper action dependent, i recommend url_for. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for

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

end

If you really want full path you pass :only_path => false to url_for.

Viktor Trón
  • 8,774
  • 4
  • 45
  • 48
0

Have you tried accessing the route with a slash rather than a full stop after charts?

i.e

/charts/week_events_bar_chart?days=14

rather than

/charts.week_events_bar_chart?days=14
Eifion
  • 5,403
  • 2
  • 27
  • 27
  • changing the path helper to: `path = chart_path(action, params)` causes the following error in the console `Started GET "/charts/week_events_bar_chart.json?days=14" for 127.0.0.1 at Tue May 22 00:50:21 +1200 2012 AbstractController::ActionNotFound (The action 'show' could not be found for ChartsController):` – jmc May 21 '12 at 12:52
  • Can you show the output from running rake routes in the terminal? – Eifion May 21 '12 at 13:07