ApplicationHelper not available to views and models?

Hello,

I have some constants that I need to share in almost all the pieces of
my application. So I decided to put them in ApplicationHelper. From what
I understand, ApplicationHelper is used to store helpers available to
the whole application.

Somehow my models and my views are unable to access the constants
defined in ApplicationHelper. My workaround for models is to ‘include
ApplicationHelper’ inside the model declaration. For views I haven’t
found a working solution aside copy’n’pasting these constants inside the
views.

What is the correct way to use ApplicationHelper and make constants
available to the entire application?


Gioele

stuff placed in application_helper.rb is only available to your
Views / Helpers.

If you want a constant to be available to everything put it in your
config/environment.rb file

Or if you only need it in your controllers controller/application.rb.

Just models you can create a module in your /lib folder and mix it in
with ActiveRecord::Base to make it available to all your models.

I know you wanted them all but I figured I’d just cover the others as
a bonus.

Hope I helped,
Jim

On May 22, 10:16 am, Gioele B. <rails-mailing-l…@andreas-

unknown wrote:

stuff placed in application_helper.rb is only available to your
Views / Helpers.
Constants in ApplicationHelper doesn’t seem to be available to my views.
Right now they are inside module ApplicationHelper, should I move them
outside?

One of my views fails with

uninitialized constant ActionView::Base::CompiledTemplates::MyConst

on

<%= options_from_collection_for_select MyConst, :first, :last %>

If you want a constant to be available to everything put it in your
config/environment.rb file
Ys, that would surely work, but, isn’t there a more “localized” way to
do it? I’d like to work inside app/ as much as possible.

I do not mind having to explicitly ‘include’ or ‘require’ stuff as long
as there are standard directories where to put these global files.

I know you wanted them all but I figured I’d just cover the others as
a bonus.
I love when I browse old forums and find complete answers like this!
Thank you.


Gioele

Hi,

now I may get corrected or put down for this, but I have put some
constants into my models where I wanted to have them available for
mapping view information against model attributes.

If you want some constants available globally (something I have been
thinking about myself), but don’t view them as being Environment type
variables, then could you not create a model Class to contain the
models and then reference them by that class. That would seem to me
to be a fairly clean approach, but someone may say that this goes
against the ‘rules’? I would be interested to know.

tonypm

On May 22, 9:27 pm, Gioele B. <rails-mailing-l…@andreas-

I’d suggest not just wholesale-including ApplicationHelper into
ApplicationController. Any public methods in controllers in Rails end
up being exposed as actions, which may have inadvertent side effects,
unless you call hide_action on them, and it’s generally better
practice to separate view and controller.

  • Gabriel

See this thread:

http://www.ruby-forum.com/topic/106831

Ignore the “crazy” talk. This shows how to create global variables.

Gioele B. wrote:

Hello,

I have some constants that I need to share in almost all the pieces of
my application. So I decided to put them in ApplicationHelper. From what
I understand, ApplicationHelper is used to store helpers available to
the whole application.

Somehow my models and my views are unable to access the constants
defined in ApplicationHelper. My workaround for models is to ‘include
ApplicationHelper’ inside the model declaration. For views I haven’t
found a working solution aside copy’n’pasting these constants inside the
views.

What is the correct way to use ApplicationHelper and make constants
available to the entire application?


Gioele

Everything I have read so far, states that the functions in the
“ApplicationHelper” module are available to all views in the
application. I guess they get loaded into the specific controller
automagically and public methods become “protected”.

So, I put a function in there, and lo and behold my view cannot see it.
Grrrr.

However, if I put the function in the helper module specific to the
view, voila, it works. This must be the “rails” magic.

So, is something is wrong, or have I not read something important?
Is there a configuration variable for this?

I’m using Rails 2.1.0.

I don’t think there is any configuration variable for this.
Any method declared in ApplicationHelper module should be available to
all
views until and unless you have done something wrong somewhere.

On Mon, Jun 30, 2008 at 2:51 AM, Polar Humenn <
[email protected]> wrote:

view, voila, it works. This must be the “rails” magic.

So, is something is wrong, or have I not read something important?
Is there a configuration variable for this?

I’m using Rails 2.1.0.

Posted via http://www.ruby-forum.com/.


Arpit J.
Senior Undergraduate Student
Department of Computer Science & Engineering
Indian Institute of Technology, Kharagpur, India.
Web: http://www.arpitjain.com

In the application controller you could put this line of code:
‘include ApplicationHelper’ in order to be able to use the helper
methods in the controller. Another approach is to put your code in
the /lib folder and do a require ‘filename’ in the controller or
model.

Gioele B. wrote:

Hello,

I have some constants that I need to share in almost all the pieces of
my application. So I decided to put them in ApplicationHelper. From what
I understand, ApplicationHelper is used to store helpers available to
the whole application.

Somehow my models and my views are unable to access the constants
defined in ApplicationHelper. My workaround for models is to ‘include
ApplicationHelper’ inside the model declaration. For views I haven’t
found a working solution aside copy’n’pasting these constants inside the
views.

What is the correct way to use ApplicationHelper and make constants
available to the entire application?


Gioele

I had a similar problem, but it was a common function. So in the models
I created global.rb:

class Global
def self.frog
return “Hello world”
end
end

Then to use it anywhere I enter
Global.frog

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs