问题
In a Rails 2.3 app I have a SitemapController
with a sitemap
action that creates a human-readable sitemap page, and a route to that in the routes file. In the public folder there's a sitemap.xml
file for search engines. The problem is that http://mysite/sitemap
is serving up sitemap.xml
, and not routing to the controller. If I delete sitemap.xml
then it routes to the controller just fine.
On several other very similar sites we have the exact same arrangement, but on those the existence of sitemap.xml
does not prevent Rails from routing to the controller. On those sites, as expected, http://mysite/sitemap
routes to SitemapController#sitemap
and http://mysite/sitemap.xml
serves the static file. Yet I've been unable to discover any difference that could be causing this problem.
Can anyone suggest what might be causing this, or how I might go about debugging it?
回答1:
This is probably a function of how you're hosting your application. Different web server environments have defaults that can affect this. For instance, Apache will tend to serve a static file at a higher priority than a call to your application, but it can be configured to not serve static files at all if you are using something like Passenger.
The way you alter this is highly dependent on the web server software you are using.
回答2:
you can do something like :
class SiteMapController
def sitemap
respond_to do |format|
format.xml { render :file => "/any/path/you/want/to/your/sitemap.xml" }
# you can then add other formats, like html, for a more human-readable response
end
end
and delete the xml in /public.
more info : http://apidock.com/rails/ActionController/MimeResponds/respond_to
来源:https://stackoverflow.com/questions/7363725/how-do-i-get-rails-to-route-to-a-controller-instead-of-a-static-file-in-the-publ