hi guys, i am quite new to RoR and i was wondering if it is possible to
achieve an automatic load of a javascript file for a view… it should
work completely magically like with the rjs files…
when i have a view ‘test.rhtml’ and there is a ‘test.js’ in the same
folder then it should include a script tag for it… does someone has a
solution or maybe this is already solved somehow within ror.
thanks in advance!!
hi guys, i am quite new to RoR and i was wondering if it is possible to
achieve an automatic load of a javascript file for a view… it should
work completely magically like with the rjs files…
when i have a view ‘test.rhtml’ and there is a ‘test.js’ in the same
folder then it should include a script tag for it… does someone has a
solution or maybe this is already solved somehow within ror.
thanks in advance!!
You could do it this way but the rails looks at the javascript folder
within the public folder for the scipt:
<%= javascript_include_tag “test” %>
Or you could do it the normal way of including the in the head, but you
should keep all your srcipts in the javascript folder, that’s why it is
there and helps keep things organized. Good luck,
thanks … but this is not exactly what i am looking for … the
javascript i want to include is a specific one for a specific view (so
each view could have its own) and therefore it make sense to have it in
the view folder … my question is how i automatically load it …
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.
thanks greg … this approach is good. it works when i add the following
line
<%=
javascript_include_tag("/#{controller.controller_path}/#{controller.action_name}.js")
if
File.exist?("#{RAILS_ROOT}/app/views/#{controller.controller_path}/#{controller.action_name}.js")
%>
the problem now is that the file is linked there but the javascript is
not loaded because its not in the “public” folder … its in the views
folder …
the problem now is that the file is linked there but the javascript is
not loaded because its not in the “public” folder … its in the views
folder …
do you have any idea?
Ah, sure. Should have seen that coming.
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?
thanks for that extensive explanation. it did help me to rethink my
strategy … i am not sure if i want to do all this just to have my JS
files included. I think i rather keep bunging the script directly into
the view.
I wonder how the guys from rails do it… i dont think they put
everything into the application.js
however, thanks a lot gregor… and its good to see that there are more
ppl out there starting with this new framework … i will check you blog
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…
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
–
Jens W., Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
Kunsthistorisches Institut der Universität zu
KölnAlbertus-Magnus-Platz, D-50923
KölnTel.: +49 (0)221 470-6668, E-Mail: [email protected] http://www.prometheus-bildarchiv.de/
thanks jens … as i see you fix the problem by placing the assets in an
own folder within the public folder. Thats a possible solution … IMHO
it would be nicer for me to have the right beside the view …
i will think which approach i will use…
thanks a lot guys!