Search code examples
ruby-on-railsrefinerycms

In Refinery CMS, why does creating a page without a title result in an uncaught exception?


As admin, I can create a page so long as it has a title. If I don't fill in the title field I get the uncaught exception below. Rather than the application crashing, I would expect the admin user to receive an error message on screen prompting him to fill in the title field.

Error

NoMethodError in Refinery/admin/pages#create 
Showing .bundler/ruby/2.0.0/refinerycms-a03fcf214281/pages/app/views/refinery/admin/pages/_form_advanced_options.html.erb where line #39 raised: 

undefined method `map' for nil:NilClass
Extracted source (around line #39): 

36:         <%= f.label :view_template, t('.view_template') %>
37:         <%= refinery_help_tag t('.view_template_help') %>
38:       </span>
39:       <%= f.select :view_template, @valid_view_templates.map { |t| [t.titleize, t] },
40:                    template_options(:view_template, @page) %>
41:     </div>
42:     <% end %>

How can I avoid this error?


Solution

  • Mixed up my Google search a little and found the answer in the Refinery Google Group:

    https://groups.google.com/forum/#!msg/refinery-cms/0FfuehWwLgA/kuCgLVlf-nsJ

    To fix this:

    1. Add a decorator file for your admin page controller at path:

    app/decorators/controllers/refinery/admin/pages_controller_decorator.rb

    2. Copy-paste the following code:

    Refinery::Admin::PagesController.class_eval do
      
      # Solves error when creating pages without title
      # See: http://stackoverflow.com/q/19013244/1093087
      before_filter :load_valid_templates, :only => [:create, :update, :edit, :new]
    
    end
    

    I now get an error message saying I need to fill in the title as expected.

    Thanks to pascal huynh at the groups.google.com link above for the quick fix.