If the js file name is based on the view name (not the names of partials), then you could have a short routine in the Layout which uses the controller :action name (which by default is the name of the view) to construct the file name you're looking for and use File.exist? to determine whether or not to render a script tag into the page head.
Well, the answer I have is probably very non-Railsy, but it's what I have done with another framework which was organized to do things like you're doing.
Conceptually you have to allow Apache (or whatever) to access the /app/views/ folder by changing the permissions.
*HOWEVER* that has some nasty security implications by allowing the source code of files in the views folder to be viewable. To counteract that, you can modify the VirtualHost directive in Apache to explicitly deny serving specific files by file extension (thus allowing .js but denying .rb, .rhtml etc). How that can be done with other HTTP servers I don't know.
I would think that approach would work, but I'm not sure at all what other, if any, security implications for Rails in particular would be.
As someone here recently said "Fight Rails, and it fights back" -- there are areas where I am figuring out how to deliver the last punch so I can shove *my opinion down its throat, but I haven't done any experimenting with this specific one yet.
I'm sure plenty of folks will hate my idea, so maybe let's wait and see if there's a better, more Railsy, idea?
thats good but this does not seperates my javascript file from
the view .. i dont want to put the javascript into the head.. i
rather want to seperate the javascript into an own file. An the
best would be if the view/controller can load this file
automatically if it is present...
i'm sorry, i didn't follow the whole thread. but we're doing
something similar, which i just refactored to also allow inclusion
of action-specific stylesheets and javascripts:
---- [app/helpers/application_helper.rb] ----
def asset_path_for(target, subtarget = nil, skip_base = false)
path = case target
when :controller
controller.controller_name
when :action
asset_path_for(controller.controller_name,
controller.action_name, true)
else
File.join(target, subtarget)
end
# We like to keep our application-specific stylesheets and
javascripts in
# a subdirectory named 'app'.
skip_base ? path : File.join('app', path)
end