rails-如何使用参数发出GET请求以执行操作 [英] rails - how to make a GET request to action with parameters
问题描述
我希望这个问题是一个相当简单的问题,因为我对Rails开发还比较陌生.我正在尝试从具有指定操作的控制器发出get请求并传递必需的参数.这是帮助程序类中的相关代码:
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
以及ChartsController中的相应动作:
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?
耙路线输出:
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"}
推荐答案
在控制台上的评论:
在导轨中的 chart_path(x,...)
将生成一条到 ChartsController#show
的路径,其参数为id = x,默认情况下为 GET/图表/x
.通过命名参数"action",您自欺欺人,"week_events_bar_chart"将只是静态路由中的ID.
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.
原始代码: charts_path(x,params)
将路由到格式为x的 ChartsController#index
,该格式类似于 GET/charts.week_events_bar_chart
,再次将其称为操作骗了你.
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.
您需要的是用于操作的命名路由帮助器 week_events_bar_chart_charts_path
.
What you need is the named route helper week_events_bar_chart_charts_path
for your action.
但是,由于您似乎希望您的帮助程序操作具有依赖性,因此我建议使用url_for. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for
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
结束
如果您确实想要完整路径,则可以通过:only_path =>false
替换为url_for.
If you really want full path you pass :only_path => false
to url_for.
这篇关于rails-如何使用参数发出GET请求以执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!