How to include a controller-specific javascript library

hey, new to RoR.
I’m having great success implementing Jack Slocum’s yui-ext
(www.jackslocum.com) with RoR.

but how can I dynamically include a controller-specific library into the
html head?

I’d like to be able to do this:

InventoryController < ApplicationController
    def index
        self.register_javascript('application/inventory/InventoryManager')
    end
end

Chris,

I am new to all this as well and haven’t really written anything in RoR
yet, so hopefully this is the correct way to do it, and if not hopefully
someone will correct me.

in your controller, use before_filter to define an instance variable:

before_filter :include

protected
def include
@jsinclude = ‘something.js’
end

Then in the application.rhtml file:

<% if @jsinclude %><%= javascript_include_tag @jsinclude %><% end %>

My only concern is if I did the if @jsinclude statement correctly as
“unless @jsinclude.empty?” throws an exception when a view is called
under another controller.

On 21-Feb-07, at 11:33 AM, Tim W. wrote:

<% if @jsinclude %><%= javascript_include_tag @jsinclude %><% end %>

My only concern is if I did the if @jsinclude statement correctly as
“unless @jsinclude.empty?” throws an exception when a view is called
under another controller.

although that solution is technical feasible, mvc encourages us to
separate concerns. This is a view concern, and as such the controller
“shouldn’t” be concerned.

why not use the layout for this controller?

Jodi

mvc encourages us to
separate concerns. This is a view concern, and as such the controller
“shouldn’t” be concerned.

I don’t think it’s correct to write-off javascript simply as “view”
concern.

the javascript lib I wish to include is a controller and has the same
name as the rails controller. it’s like a client-side agent of the
controller.

eg:
InventoryController.rb < ApplicationController

InventoryController.js

Jodi S. wrote:

why not use the layout for this controller?

That makes sense.

So now since application.rhtml is not used, the best practice would be
to create partials for the header and footer information that was
previously displayed via application.rhtml ?

On Feb 21, 11:58 am, “Tim W.” [email protected]
wrote:

Posted viahttp://www.ruby-forum.com/.
or you could have the application layout check the controller name,
and return the JS include. This is probably a good use for a helper - <
%= get_JS(controller.controller_name) %>

I’d likely tackle by that means - the application helper might even
have scope to access the controller object, so your call would just be
“get_JS”

On 2/22/07, Chris S. [email protected] wrote:

name as the rails controller. it’s like a client-side agent of the
controller.

eg:
InventoryController.rb < ApplicationController

InventoryController.js

I wrote a plugin a while ago just for this.

You can find details for it at

http://www.agilewebdevelopment.com/plugins/resource_on_demand

Hopefully it will do what you’re after

I’d likely tackle by that means - the application helper might even
have scope to access the controller object, so your call would just be
“get_JS”

cheers. that’ll work.

I usually just build up an array within my controller such as:

@javascript_includes = [‘prototype’,‘scriptaculous’,‘other_file’]

and then in my layout have:

<%=javascript_include_tag *@javascript_includes -%>

Regards,

Glenn

Hopefully it will do what you’re after

exactly. ensures no duplicates. perfect, thanks.