Reputation: 69
I think this should be a simple question. I would like to route a URL of the form:
http://mywebsite.com/my_special_tag
to a specific controller/action pair with a specific id, e.g.
http://mywebsite.com/user/post/13
(where 13 is the id I want to pass into the controller/action).
Note also that I don't want to redirect, just want to render.
Is there an easy way to do this in Rails 3?
Thanks!
Upvotes: 0
Views: 1729
Reputation: 26193
Assuming your know specifically which URL you want to have rendered to, you can explicitly match to the target in your routes:
# config/routes.rb
match 'my_special_tag' => 'user#post', :defaults => {:id => 13}
Note that this necessarily requires that you have a post
action within a UserController
(note that the names are singular, as they are in your desired path).
Upvotes: 1
Reputation: 4229
If you check your routes you should get the syntax for a user post route, if you have defined the nested route correctly.
To alias the route, you can define a specific id for a particular match like so:
match '/special_tag' => 'posts#index', :defaults => { :id => '5' }
Update
To clarify, the above should only be used for a couple of specific pages you'd like to alias a route for. If you want to provide a more user friendly link to each user post page, I'd recommend something like https://github.com/norman/friendly_id which allows you to have routes like
http://example.com/states/washington
instead of:
http://example.com/states/4323454
Upvotes: 0
Reputation: 19294
You simply need a table like this:
tags table:
id | post_id | tag
-------------------
1 | 13 | my_special_tag
And then you need a before_filter
, that takes that tag and checks the database for the post_id.
You'd also need a wildcard route that looks something like this: Rails 3.1 - in routes.rb, using the wildcard, match '*' is not catching any routes
Start with that and then let us know when you have more specific questions and we can help you better.
Upvotes: 0