简体   繁体   中英

Ruby on Rails, How to make a controller render different page without redirect?

Let's say we have the following pages clip/show/111 image/show/222 sound/show/333 where 111,222 and 333 are the IDs for different contributions

I developed a new controller called "show" that will detect ID and redirect to the right controller show/show/111 will redirect to clip/show/111 and so on

what I want is to render the page clip/show/111 without redirecting, meaning that the URL on the browser will still show "show/show/111" but in fact render the "clip/show/111"

How could I do that?

Thanks all Wa'el

It sounds like what you're really trying to do is make aliases for some of your routes, so you can access the same functionality from different URL structures. If that is the case, creating an entire controller that delegates all of its work to another controller would be a code smell (feature envy).

What you probably want to do is define some extra routes for "show/" that simply re-use the same Clip controller. This would be accomplished by simply doing the following:

# routes.rb
# ...
# For Rails 2
map.resources "show", :controller => "clip"

# For Rails 3
resources "show", :controller => "clip"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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