Controller method problem

Hi,

I have a def in my controller see below where i want to find the page
object based on a parameter value and then use the page_id to then find
all the contents that belong to that page in the same method.

Controller
def aboutus
@page = Page.find_by_name(params[‘About Us’])
@contents = Content.find(:all,
:conditions => “page_id = #{@page.id}”)
end

Page Model
def self.find_by_name(pagename)
find(:first,
:conditions => “name = ‘#{pagename}’”)
end

I keep getting an error message “Called id for nil”. Can anyone let me
know what i’m doing wrong here.

On 8/14/06, Jeff C. [email protected] wrote:

returns all matches.
For all the gory details…

http://blog.hasmanythrough.com/articles/2006/08/13/how-dynamic-finders-work


Josh S.
http://blog.hasmanythrough.com

John B. wrote:

Hi,

I have a def in my controller see below where i want to find the page
object based on a parameter value and then use the page_id to then find
all the contents that belong to that page in the same method.

Controller
def aboutus
@page = Page.find_by_name(params[‘About Us’])
@contents = Content.find(:all,
:conditions => “page_id = #{@page.id}”)
end

I keep getting an error message “Called id for nil”. Can anyone let me
know what i’m doing wrong here.

Sounds like @page is nil. Are you sure you params[‘About Us’] contains
a valid page name?

Page Model
def self.find_by_name(pagename)
find(:first,
:conditions => “name = ‘#{pagename}’”)
end

Nice, but Rails already did this for you :slight_smile: ActiveRecord objects will
create dynamic finders on the fly for you if you follow a certain
convention:

model.find_by_column(‘value_to_search_for’)

returns the first match, and

model.find_all_by_column(‘value_to_search_for’)

returns all matches.

Jeff
softiesonrails.com

On 8/14/06, John B. [email protected] wrote:

I have a def in my controller see below where i want to find the page
object based on a parameter value and then use the page_id to then find
all the contents that belong to that page in the same method.

Controller
def aboutus
@page = Page.find_by_name(params[‘About Us’])

Do you really have a param named “About Us”? Are you sure you aren’t
trying to pass “About Us” into that method?

– James

James L. wrote:

On 8/14/06, John B. [email protected] wrote:

I have a def in my controller see below where i want to find the page
object based on a parameter value and then use the page_id to then find
all the contents that belong to that page in the same method.

Controller
def aboutus
@page = Page.find_by_name(params[‘About Us’])

Do you really have a param named “About Us”? Are you sure you aren’t
trying to pass “About Us” into that method?

– James

Thanks guys.

Yes your right, the param is not called About Us i was just hard coding
that value just to make sure the value i wanted was being passed to the
model. There was nothing coming from the model thats why i was getting
the nil error message.

@page = Page.find(:first,:conditions => “name = ‘About Us’”)
@contents = Content.find(:all,
:conditions => “page_id = #{@page.id}”,
:order => ‘pos, position_id’)

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