Hi
I have a model called navbar. It does not have a view or a controller
defined.
create_table :navbars do |t|
t.string :name
t.string :display_name
t.string :url
t.string :group_by_module
t.boolean :default_link
t.integer :sort_order
And in the model I have written few methods.
class Navbar < ActiveRecord::Base
def all_modules
NavBar.find (:display_name, :url, :conditions => {:name =>
‘module_bar’}, :order => ‘sort_order’)
end
def associated_quick_links (selected_module)
NavBar.find (:display_name, :url, :default_link, :conditions =>
{:name => ‘quick_links_bar’, :group_by_module =>
selected_module}, :order => ‘sort_order’)
end
def associated_module (selected_quick_link)
NavBar.find (:group_by_module, :conditions => {:name =>
‘quick_links_bar’, :display_name => selected_quick_links})
end
end
QUESTION — I want to access these methods from views directly. When
I do <%= debug navbar.all_modules %> it fails. It cannot find the
navbar.
Can someone please guide me?
Thanks in advance
For accessing model method from view , you can define self methods in
model
and than directly access by ModelName.methodName
For example
class Navbar < ActiveRecord::Base
def self.all_modules
Ur stuff
end
end
in view
<% Navbar.all_modules %>
~
N A R E N
Did what you said…
Now getting error — “undefined local variable or method `navbar’ for
#ActionView::Base:0x4dfce0c”
On Jun 15, 10:54 pm, Narendra sisodiya [email protected]
can you tell me what you did exactly ?
Hi
QUESTION — I want to access these methods from views directly. When
I do <%= debug navbar.all_modules %> it fails. It cannot find the
navbar.
This will work fine only if you have navbar as an object of your Navbar
model, confirm whether you have done the same.
As I observe that your method in model does not have anything related to
specific object of the class, you can have it as a class method by
defining
it as self.all_modules. This can be directly used by Navbar.all_modules
Note: One should not use model methods in views directly. Use controller
objects or helpers for the same.
On Tue, Jun 16, 2009 at 10:55 AM, Narendra sisodiya <
[email protected]> wrote:
On Jun 15, 10:54 pm, Narendra sisodiya [email protected]
end
t.integer :sort_order
{:name => ‘quick_links_bar’, :group_by_module =>
navbar.
Can someone please guide me?
Thanks in advance
–
agree with Nayak,
If you didn’t create the object navbar than it will through the
exception
that is in your case.
or define static methods like I did and use directly by model name.
Avoid direct model’s method calls in view , use helper file or
controller as
suggested by Nayak.
~N A R E N
This is like a reference table which doesnt need a controller
And it is a reftable for what links to display on screen.
This is what I did finally.
class Navbar < ActiveRecord::Base
def self.all_modules
NavBar.find (:display_name, :url, :conditions => {:name =>
‘module_bar’}, :order => ‘sort_order’)
end
def self.associated_quick_links (selected_module)
NavBar.find (:display_name, :url, :default_link, :conditions =>
{:name => ‘quick_links_bar’, :group_by_module =>
selected_module}, :order => ‘sort_order’)
end
def self.associated_module (selected_quick_link)
NavBar.find (:group_by_module, :conditions => {:name =>
‘quick_links_bar’, :display_name => selected_quick_links})
end
end
And I am still getting
undefined local variable or method `navbar’ for #<ActionView::Base:
0x4be1140>
Extracted source (around line #2):
1:
2: <%= debug navbar.all_modules %>
Please help
On Jun 16, 1:04 am, Narendra sisodiya [email protected]
ok let me explain you in your code
following is your code
1:
2: <%= debug navbar.all_modules %>
now the navbar is undefined , please replace navbar to Navbar
capital Nthat is your Model name
see in this line
class *Navbar *< ActiveRecord::Base
now try this and let me know
~N A R E N
In the controller for the action you are testing, did you create an
instance of your Navbar class? You are defining a Navbar class, but a
class without an instance is fairly useless… Something like:
@navbar = Navbar.new
If so, then the view code as you had it:
<%= @navbar.all_modules %>
should have an instance of Navbar to work with. Your all_modules method
inside Navbar cons a little dubious though…
On Jun 16, 12:16 pm, Ritvvij [email protected] wrote:
<%= debug NavBar.all_modules %>
According to your previous posts the class is called Navbar, so you
need to type exactly that. Not navbar, NavBar, NAvBaR or anything like
that.
Fred
NavBar is a reference table whose content needs to be accessed in all
VIEWS.
This includes the application layout too.
Can you please guide which is the best way to model this??? + where to
initialize an instance?
Well… I am using it in application.html.erb --> layouts.
So where should I create the instance?
Have you tried it this way yet??? (capital N only!)
<%= debug Navbar.all_modules %>
If you want to initialise the Navbar instance somewhere that has to be
accessed by all views (or the layout), put it in the application
controller as a before_filter. For example:
class ApplicationController < ActionController::Base
Some other stuff
before_filter :get_navbar
def get_navbar
@navbar = Navbar.new
end
More stuff
end
Then in you view or layout you can access methods using the syntax as
outlined earlier:
<%= @navbar.all_modules %>