Custom fields in Refinery CMS

前端 未结 2 1689
悲哀的现实
悲哀的现实 2021-01-27 06:04

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,

相关标签:
2条回答
  • 2021-01-27 06:24

    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

    0 讨论(0)
  • 2021-01-27 06:39

    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.

    0 讨论(0)
提交回复
热议问题