0

How do you add custom fields in Refinery CMS? That is, I would like to extend the basic page model to include a bunch of other stuff, like screenshots, publisher name, category, and assorted other fields. How do you implement that?

Avishai
  • 4,512
  • 4
  • 41
  • 67

2 Answers2

7

To expand a base model of refinery, generating an engine is not always an option.

I expanded the page model by a date field by

  • migrate the pages table
  • monkey patch the Page model
  • add (in fact expand) a partial in the form

in detail:

rails g migration AddDateToPage event_date:date
rake db:migrate

add app/models/page.rb with

require Refinery::Pages::Engine.config.root + 'app' + 'models' + 'page'
class Page
  attr_accessible :event_date
end

it expands the Page model so that my custom field is set by mass assignment (thanks to http://railsrx.com/2011/04/15/overriding-refinery-extending-globalize-and-pow/ for the require statement).

Now create the file app/views/admin/pages/_form_fields_after_title.html.erb which is a stub provided by refinery. You can also create it with

rake refinery:override view=admin/pages/_form_fields_after_title

here I defined the form field (f is set with a form_for object by refinery)

<div class="field">
  <%= f.label :event_date, t('admin.pages.event_date') %>
  <%= f.text_field :event_date %>
</div>

After restarting the application, the new field is active.

Martin M
  • 8,430
  • 2
  • 35
  • 53
2

Have a look at the (excellent) Getting Started with Refinery guide. In Section 6 the guide lays out how to add extra fields - in the example date, picture and blurb - using engines:

http://refinerycms.com/guides/getting-started-with-refinery#extending-refinery-with-your-first-engine

Alex Peattie
  • 26,633
  • 5
  • 50
  • 52