refinerycms icon indicating copy to clipboard operation
refinerycms copied to clipboard

Extending Page model with a resource "undefined method `url'".

Open curatingbits opened this issue 11 years ago • 1 comments

Not really sure if this is an issue or if it's something I'm missing. I followed the extending a page model doc but instead of an image I want to use a file(resource). Here is my migration:

class AddPdfDocToRefineryPages < ActiveRecord::Migration
  def change
    add_column :refinery_pages, :pdf_document_id, :integer
  end
end

In the models page_decorator.rb file here is what I have:

Refinery::Page.class_eval do
  # Whitelist the :background_image_id parameter for form submission
  # attr_accessible :background_image_id

  # Add an association to the Refinery::Image class, so we
  # can take advantage of the magic that the class provides
  belongs_to :pdf_document, :class_name => 'Resource'
end

I added the pdf_document_id to page_params directly into the /controllers/refinery/admin/page_controller.rb instead of creating a decorator under models:


 def page_params
        params.require(:page).permit(
          :browser_title, :draft, :link_url,:pdf_document_id, :menu_title, :meta_description,
          :parent_id, :skip_to_first_child, :show_in_menu, :title, :view_template,
          :layout_template, :custom_slug, parts_attributes: [:id, :title, :body, :position]
        )
      end

Updated the pages form to use the resource picker:

    <div class="field">
      <%= f.label :pdf_document%>

      <%= render :partial => "/refinery/admin/resource_picker", :locals => {
                                                                   :f => f,
                                                                   :field => :pdf_document_id,
                                                                   :resource => f.object.pdf_document,
                                                                   :toggle_image_display => false
                                                           }
      %>
    </div>

Now here is what is strange. In the view I'm just displaying the link location of the resource for now with a simple:


   <%= @page.pdf_document.url %>

In development it will render the show page, and show the url on the page, but in the logging it throws an undefined method 'url' (see below). In production, it doesn't render the page and throws an error.

ActionView::Template::Error (undefined method `url' for nil:NilClass):
    60: 
    61:             <!--<div id="pdf">It appears you don't have Adobe Reader or PDF support in this web browser.  <#%= link_to("Click Here to download the PDF", @page.pdf_document.url) %></div>-->
    62: 
    63:             <%= @page.pdf_document.url %>
    64: 
    65:             <div class="row">
    66:               <div class='list-group gallery'>
  app/views/refinery/pages/show.html.erb:63:in `_app_views_refinery_pages_show_html_erb__1254988805318126826_2276564160'
  app/controllers/refinery/pages_controller.rb:78:in `find_page'


  Rendered /Users/duggan1/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (8.0ms)
  Rendered /Users/duggan1/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
  Rendered /Users/duggan1/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
  Rendered /Users/duggan1/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (22.8ms)

Here is an image proving that it does render the show page in development correctly, with the correct url. events_-_upcoming_events

Any ideas on what might be going on? This is my last todo item on the project and I'm done! I appreciate your time and hopefully someone can help me.

Thank you!

curatingbits avatar Jun 30 '15 18:06 curatingbits

I managed to get rid of the error by using the try method.

<%= @page.pdf_document.try(:url) %>

curatingbits avatar Jul 07 '15 04:07 curatingbits