Displaying "Site Title" from MySQL Throughout Entire Site

I’m an absolute noob, so please excuse my ignorance here :slight_smile:

I’m looking for away to display the site’s title that I’ve entered into
a mysql database.

Assuming I created the table using this:

create table settings (
id int not null auto_increment,
yourname varchar(100) not null default ,
sitetitle varchar(100) not null default ,
primary key(id)
) engine=InnoDB;

How would I fetch that sitetitle to display between the
tags from the view pages?

In your application controller you could do someting like

before_filter :get_site_title

private
def get_site_title

end

On Jun 13, 9:58 am, Bob S. [email protected]

Oops, sorry google groups decided to post while I was still writing
(maybe I accidentally hit a key combination to post:(). Anyway…

In your application controller you could do something like

before_filter :get_site_title

private
def get_site_title
@site_title = Settings.sitetitle
end

and in your layout

<%= @site_title %>

This means you are adding an extra db query to every page though.
Since the site title is not going to change do you really need to
store it in the database?

Cheers,

AF

Hi Aidan!

Thanks for your help :slight_smile:

I followed your instructions, and I’m running into this error:

================================

NameError in ClientController#index

uninitialized constant ApplicationController::Settings

================================

I incorporated the:

before_filter :get_site_title

private
def get_site_title
@site_title = Settings.sitetitle
end

into the application.rb controller right underneath the “class
ApplicationController < ActionController::Base”

Do you know what I might be doing wrong?

On 6/13/07, aidanf [email protected] wrote:

@site_title = Settings.sitetitle
end

That’s not going to work. You need something like:

@site_title = Settings.find(:first).sitetitle rescue “Default Title”

He might want to just get all the settings:

@settings = Settings.find(:first) || Settings.new

Then in the view:

<%= h @settings.sitetitle %>

This means you are adding an extra db query to every page though.
Since the site title is not going to change do you really need to
store it in the database?

Cheers,

AF

I forgot to answer your question. My apologies!

I’m trying to play around with ROR as much as I can to get a flow of how
things work, so I thought about incorporating a feature where the user
can change the site’s title.

Awesome, Bob! Thank you so much. Thank you, thank you, thank you. You’re
the greatest! :slight_smile:

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