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!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michal Gabrukiewicz wrote:> 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 <script src = "/javascript/test.js" type="text/javascript"> 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, -S -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 15, 2007, at 7:55 AM, Michal Gabrukiewicz wrote:> 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!!The techniques for detecting and loading files in this article would apply. http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/ 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. -- gw (www.railsdev.ws) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 .. do you have any idea? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 15, 2007, at 2:16 PM, Michal Gabrukiewicz wrote:> 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 .. > > 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? -- gw (www.railsdev.ws) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 - good posts! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
There is another alternative: <%# application.rhtml.erb %> <%= javascript_include_tag(yield :javascripts) if yield(:javascripts) %> <%# my_random_view.rhtml.erb %> <% content_for :javascripts do -%>some_other_javascript_file<% end -%> That should allow you to set the javascript in the head element where it belongs without being too hacky. Of course, you can do it in the controller with a simple: @content_for_javascripts = ''some_other_javascript_file'' --s On Nov 15, 2007, at 4:20 PM, Michal Gabrukiewicz wrote:> > 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 > - good posts! > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Steve Ross wrote:> There is another alternative: > > <%# application.rhtml.erb %> > > <%= javascript_include_tag(yield :javascripts) if yield(:javascripts) %> > > > <%# my_random_view.rhtml.erb %> > > <% content_for :javascripts do -%>some_other_javascript_file<% end -%> > > That should allow you to set the javascript in the head element where > it belongs without being too hacky. > > Of course, you can do it in the controller with a simple: > > @content_for_javascripts = ''some_other_javascript_file'' > > --sthats 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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hi michal! Michal Gabrukiewicz [2007-11-16 02:13]:> 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 def stylesheet_link_tag_for(target, subtarget = nil) path = asset_path_for(target, subtarget) stylesheet_link_tag(path) \ if File.exists?("#{RAILS_ROOT}/public/stylesheets/#{path}.css") end def javascript_include_tag_for(target, subtarget = nil) path = asset_path_for(target, subtarget) javascript_include_tag(path) \ if File.exists?("#{RAILS_ROOT}/public/javascripts/#{path}.js") end ---- snip ---- and then in your layout: ---- [app/views/layouts/application.rhtml] ---- <%= javascript_include_tag_for :action %> ---- snip ---- this should be quite self-explanatory ;-) cheers jens -- Jens Wille, Dipl.-Bibl. (FH) prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre Kunsthistorisches Institut der Universität zu Köln Albertus-Magnus-Platz, D-50923 Köln Tel.: +49 (0)221 470-6668, E-Mail: jens.wille-31N1O1AsgN5n68oJJulU0Q@public.gmane.org http://www.prometheus-bildarchiv.de/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! michal - http://webdevbros.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---