views:

101

answers:

1

Hi All. I seem to have got a little a head of myself. I have created a Page Model and Pages controller. The whole idea was to be able to call something like 'print :controller=>'Pages', :action=>'view', :id=>'6', :layout=>'none''

And in my application.html.erb I have a div with yield, and in the next div I would like to have the above page.

But I just can't get my head around doing this. Anyone who understands what I am trying to do ? As simply as I can explain this is that I am trying to store static text in a database, and being able to call it when ever I need to. So I can store whole pages like 'hello' and 'about me' in a text field, kind of a built in cms

Trausti

+2  A: 

I think you're failing to grok the flexibility of Rails models. They're just object classes. You can use them anywhere you'd use a Ruby object. All of that :controller => 'blah', :action => 'yadda' stuff is for generating or parsing URLs for the outside world. Inside your app, just call on your model.

In this case, if all you want is to easily pull the content from a page with a known name, my suggestion would be to write a class method within your Page class that does the finding for you. For instance (adjust to your own field names and needs):

class Page < ActiveRecord::Base
  # ...Other stuff...

  def self.content(name)
    page = find_by_name(name)
    page.content if page
  end
end

Then you could just call Page.content('about') anywhere you needed it and it'd return the content of the 'about' page, hot and fresh for you.

SFEley
oh man. So obvious once you see it. Thanks so much this is exactly what I needed.
Trausti Thor Johannsson