How do i get Rails to route to a controller instea

2019-06-01 17:06发布

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?

2条回答
Summer. ? 凉城
2楼-- · 2019-06-01 17:31

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.

查看更多
家丑人穷心不美
3楼-- · 2019-06-01 17:31

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

查看更多
登录 后发表回答