Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 访问从控制器代码传递到URL的参数_Ruby On Rails_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 访问从控制器代码传递到URL的参数

Ruby on rails 访问从控制器代码传递到URL的参数,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,我的Rake路线如下所示: report GET /management/:id/report(.:format) report#show 在控制器代码中,如何访问:id参数?它仍然是通过.params[:id]实现的吗 我觉得我很困惑,因为这个时间ID不是URL中的最后一个,它在URL的中间。 是的,您可以访问您在路由中定义的参数,如您所说: # The route # /management/:id/report(.:format) # will generate the followi

我的Rake路线如下所示:

report GET /management/:id/report(.:format) report#show
在控制器代码中,如何访问:id参数?它仍然是通过
.params[:id]
实现的吗


我觉得我很困惑,因为这个时间ID不是URL中的最后一个,它在URL的中间。 是的,您可以访问您在路由中定义的参数,如您所说:

# The route 
# /management/:id/report(.:format)
# will generate the following params:
params[:id]
params[:format] # optional
另一个例子:

match ':controller(/:action(/:id))'
# will produce the following params:
params[:controller]
params[:action] # (optional)
params[:id] # (optional)

match '/search/:search'
# will produce, in the SearchController (and views):
params[:search]

它是
参数[:id]
。它只是一个命名参数,不必是url的最后一部分。如果它不起作用,请提供您的路线。

那么它是通过参数的“名称”起作用的吗?如果我把它命名为“provider\u id”,我可以在代码中调用它吗?那很酷。我理解对了吗?是的,你理解对了@user1899082!您可以根据需要命名参数;)