简体   繁体   English

通过Rails中的RESTful路由将变量传递给Controller

[英]Passing variables to Controller via RESTful routing in Rails

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. 我有一个带有一个模型的Ruby on Rails(v 2.3.3)应用程序,该应用程序以多种视图交付给用户。 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). 我找到了一种使用非RESTful路由(map.with_options等)的方法,但是我更喜欢将RESTful路由与map.resource一起使用。 One ugly solution might be to parse the request URI in the controller, which I'd not prefer. 一种丑陋的解决方案可能是在控制器中解析请求URI,我不希望这样做。

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: 您很可能希望拥有不同的索引,为此只需使用collection get:

 map.resources :episodes, :collection => {:simpsons=> :get, :flintstones=> :get} do |episode|
 ....and so on

Simply add the methods named "simpsons" and "flintstones" in your controller. 只需在控制器中添加名为“ simpsons”和“ flintstones”的方法即可。 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. 并且,对于编辑,查看和删除方法,如果需要,可以通过确定当前条目的ID来使用一些额外的逻辑。 Something like @episode = Episode.find(params[:id]), if @episode.criteria == ...something... then render this or the other view. 类似于@episode = Episode.find(params [:id]),如果@ episode.criteria == ...某物...则呈现此视图或另一个视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM