0

我希望这个问题是一个相当简单的问题,因为我对 Rails 开发比较陌生。我正在尝试从具有指定操作的控制器发出获取请求并传递所需的参数。这是助手类中的相关代码:

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

以及 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

路由文件有以下内容:

resource :charts do
    get 'week_events_bar_chart'
end

但是,在尝试执行此请求时,我得到以下输出:

 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"}

并且永远不会调用控制器操作。有人能帮助解决这个问题吗?

编辑:耙路线输出:

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"}
4

2 回答 2

1

对于您对控制台的评论:

在 rails 中chart_path(x,...)会生成一个到ChartsController#showparam id = x 的路由,默认情况下是GET /charts/x. 通过将参数命名为“action”,您自欺欺人了,“week_events_bar_chart”将只是 restful 路线中的 id。

到您的原始代码: charts_path(x, params)ChartsController#index使用格式 x 路由到,看起来像GET /charts.week_events_bar_chart,再次称它为动作愚弄了您。

您需要的是week_events_bar_chart_charts_path为您的操作命名的路由助手。

但是由于您似乎希望您的辅助操作依赖,我推荐 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

结尾

如果您真的想要完整路径,请传递:only_path => false给 url_for。

于 2012-05-21T13:46:59.910 回答
0

您是否尝试过使用斜线而不是句号来访问路线charts

IE

/charts/week_events_bar_chart?days=14

而不是

/charts.week_events_bar_chart?days=14
于 2012-05-21T12:46:13.613 回答