I'm new to Rails and have run into a roadblock with routes in a small app I'm writing -
I would like to use the root route to to map parameters to a particular controller -
For example, lets say I have a controller, 开发者_开发百科VariablePageController, that will render different content based on the parameter in the URL.
So when someone visits my site, mysite.com will go to a home page, but mysite.com/[pagename] will go through my VariablePageController, but the user will stay at mysite.com/[pagename] and NOT mysite.com/VariablePage/[pagename] -
The variable [pagename] is dynamically generated, so I can't list all possibilities in the routes.rb...
How would achieve this effect with routing?
Alright, I found the answer, for anyone interested...
What I essentially wanted, but I guess didn't describe well, was 'slug-style' or 'friendly' urls based off of the URL root rather than a /controller/ route.
To accomplish this - do something like this in routes.rb: Standard Home Page Route
root :to => "page#index"
Parameter-Based Home Route
match ':url' => "page#show"
You can do a simple named route like
match '/pagename' => "variablepage#pagename", :as => "pagename"
then your link would look as follows
<%= link_to pagename, pagename_path %>
or
<%= link_to "pagename", pagename_path %>
after that in your variablepage controller you have
def pagename
.....
end
i hope this helps you
精彩评论