views:

20

answers:

2

I have two identical controllers Submissions and Activities, both using the default resource mapping for routes. I want to use some RJS for the show method, both of which can be accessed as such as a GET request:

/submissions/1.js
/activities/1.js

I have show.rjs inside their respective view folders like so:

../submissions/show.rjs
../activities/show.rjs

Out of the box, this works for activities but not for submissions. To fix it, I had to rename show.rjs to show.js.rjs.

Why is this so? I have noticed a similar occurrence for HTML files but am not able to reproduce it. Is there some configuration apart from the routes that I am unaware of that is doing this?

Thanks.

A: 

Since Rails 2.X there is .js.erb for javascript and .html.erb for HTML and so on. You should use these extensions.

klew
I get it that there is a file naming convention. But what triggers this and why is it that it works for one controller and not the other?
Jaryl
I don't know. But does it work if both has `.js.erb`?
klew
I used .js.rjs and yes it works for both controllers. I just don't get why one controller requires the extension, while the other controller doesn't.
Jaryl
A: 

It is related to ActionController responds_to:

Rails determines the desired response format from the HTTP Accept header submitted by the client.

If you are using RESTful controllers you need a respond_to block in the controller action with a view file for each response type with the correct file extension pattern.

http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html

inkdeep
I know how to route them, which is how I got here. My question is why one controller requires the extension to be explicit in the template file name, while others do not, given the same route declaration.
Jaryl
There is no rails convention that causes this behavior by design as far as I know. Did one controller have more than one file named show (show.html.erb and show.rjs)? Are the respond_to blocks in each controller exactly the same?
inkdeep