通过 Rails 中的 RESTful 路由将变量传递给控制器
以下问题与将变量从路由传递到控制器有关。我有一个带有一个模型的 Ruby on Rails (v 2.3.3) 应用程序,该应用程序通过多个视图交付给用户。当前的解决方案涉及使用由多个路由触发的多个控制器。例如:
ActionController::Routing::Routes.draw do |map| # defines map
map.resource :simpsons, :only => [] do |b|
b.resources :episodes, :controller => "SimpsonsEpisodes"
end
map.resource :flintstones, :only => [] do |b|
b.resources :episodes, :controller => "FlintstonesEpisodes"
end
但是,为了保持干燥,我希望这些路由能够使用相同的控制器进行操作。为了让控制器区分路线,我想通过路线传递一个变量。例如:
map.resource :simpsons, :only => [] do |b|
b.resources :episodes, :controller => "Episodes", :type => "simpsons"
end
map.resource :flintstones, :only => [] do |b|
b.resources :episodes, :controller => "Episodes", :type => "flintstones"
end
所以在控制器中我可以这样做:
case(type)
when "simpsons" then ... do something for the Simpsons ...
when "flintstones" then ... do something for the Simpsons ...
else .... do something for all episodes ....
end
我找到了一种使用非 RESTful 路由(map.with_options 等)来做到这一点的方法,但我更喜欢将 RESTful 路由与 map.resource(s) 一起使用。一种丑陋的解决方案可能是在控制器中解析请求 URI,这是我不喜欢的。
The following question is related to passing a variable from routes to the controller. I have a Ruby on Rails (v 2.3.3) app with one model, which is delivered to the user with multiple views. The current solution involves using multiple controllers which are triggered by multiple routes. For example:
ActionController::Routing::Routes.draw do |map| # defines map
map.resource :simpsons, :only => [] do |b|
b.resources :episodes, :controller => "SimpsonsEpisodes"
end
map.resource :flintstones, :only => [] do |b|
b.resources :episodes, :controller => "FlintstonesEpisodes"
end
However, for the sake of DRYness I would like these routes to operate with the same controller. In order for the controller to distinct between the routes I would like to pass along a variable via the route. For example:
map.resource :simpsons, :only => [] do |b|
b.resources :episodes, :controller => "Episodes", :type => "simpsons"
end
map.resource :flintstones, :only => [] do |b|
b.resources :episodes, :controller => "Episodes", :type => "flintstones"
end
So in the controller I could do this:
case(type)
when "simpsons" then ... do something for the Simpsons ...
when "flintstones" then ... do something for the Simpsons ...
else .... do something for all episodes ....
end
I found a way to do this with non-RESTful routing (map.with_options etc.), but I'd prefer to use RESTful routes with map.resource(s). One ugly solution might be to parse the request URI in the controller, which I'd not prefer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于没有回复并且我找到了解决方案,我将自己回答这个问题。如果您还遇到一种情况,您可能有一个模型或一个表,其中有多个条目,但对于其中一些条目,您可能需要单独的视图,这可能会给您带来一些好处。
您很可能希望拥有不同的索引,为此只需使用 collection get:
只需在控制器中添加名为“simpsons”和“flintstones”的方法即可。而且,对于编辑、查看和删除方法,如有必要,您可以通过确定手头条目的 ID 来使用一些额外的逻辑。像 @episode = Episode.find(params[:id]) 之类的东西,如果 @episode.criteria == ...某事...然后渲染这个或另一个视图。
Since there are no replies and I found a solution, I am going to answer this question myself. If you also have a situation, where you might have a model or a table, which has multiple entries, but for some of them you might need separate views, this might benefit you a bit.
Most likely you would like to have different indexes and for that simply use collection get:
Simply add the methods named "simpsons" and "flintstones" in your controller. And, for the edit, view and delete methods you can use a bit of extra logic, if necessary, by determening the ID of the entry at hand. Something like @episode = Episode.find(params[:id]), if @episode.criteria == ...something... then render this or the other view.