Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

rest - Methods for limiting the Rails render format to html

I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so when I request a restful resource:

www.mysite.com/jobs/1

It renders my job in html, however, if I request:

www.mysite.com/jobs/1.xml

I get the error:

Template is missing Missing template jobs/show.xml.erb in view path c:/workspace/mysite/app/views

What's worse is that I can also request something like

www.mysite.com/jobs/1.xyz

And indeed I see the error:

Template is missing Missing template jobs/show.xyz.erb in view path c:/workspace/mysite/app/views

To stricly present just html content, what is the cleanest and simplest way to tell Rails that I don't want to render anything other than .html.erb files.

It is important to note that:

  • Some of my controller actions contain conditional calls to the render() method whilst others use the default Rails behaviour i.e. if you don't call render() then the template named youraction.html.erb will be rendered.
  • My code does not use the responds_to() method

It would be great if the solution was not at the render/responds_to level as I would have to modify a significant number of actions. Perhaps there is a way to configure Rails so that only html templates are rendered?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your routes you can simply remove the line:

map.connect ':controller/:action/:id.:format'

And the ".xyz" will no longer be routed, resulting in 404 errors/.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...