small medium large xlarge

Generic-user-small
05 Oct 2008, 14:14
James West (104 posts)

Sorry for the really dumb question but I am still failing to understand some of the fundamentals of rails.

I have scaffolded a new table called design with the intention of placing a some configuration data in a single record such as aplication_title.

I have placed code in the store.html.erb to call a method in the application_helper and my now looks like this

<%= yield(:title) || "Title not set" %><!-- <label id="code.depot.e.title"/> -->

My application_helper.rb now looks like this

module ApplicationHelper
  def title(page_title)
    content_for(:title) {get_title_from_database || page_title}
  end

private

  def get_title_from_database
    "A title from the database"
  end
end

If I just put a string constant into the get_title_from_database method everything works a treat so now here comes the biggy

I want to call the get_title method that I have in my designs.rb model.

How do I do this without getting some sort of uninitialized constant error?

Using Delphi,VB,C# and the likes of, I would have a variable of type designs that I would create an instance of, use the method then destroy the instance.

But I just can’t seem to get my head round how rails would do this.

My design.rb looks like this

class Design < ActiveRecord::Base
  def get_title_from_database
    Designs.find(:first)
  end
end

I know this needs fleshing out to actually return the value of the application_title field but in theory I should be able to call Design.get_title_from_database but it just does not work.

I would like my application_helper to look something like this

module ApplicationHelper
  def title(page_title)
    content_for(:title) {get_title_from_database || page_title}
  end

private

  def get_title_from_database
    Design.get_title_from_database
  end
end

However, this gives me the following error

NoMethodError in Store#index

Showing store/index.html.erb where line #1 raised:

undefined method `get_title_from_database’ for #

I guess it boils down to a lack of understanding on my part so any help would be greatly appreciated.

Generic-user-small
05 Oct 2008, 17:47
James West (104 posts)

It’s OK I’ve got it.

module ApplicationHelper
  def title(page_title)
    content_for(:title) {get_title_from_database || page_title}
  end

private

  def get_title_from_database
    @design = Design.new
    @design.get_title_from_database
  end
end

Works a treat. Sorry for the silly question!

You must be logged in to comment