Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapped dynamic entries "undefined method" error. #104

Closed
Jonic opened this issue Sep 19, 2016 · 2 comments · Fixed by #113
Closed

Mapped dynamic entries "undefined method" error. #104

Jonic opened this issue Sep 19, 2016 · 2 comments · Fixed by #113

Comments

@Jonic
Copy link

Jonic commented Sep 19, 2016

I've been trying to set up a mapped entry with dynamic_entries: :auto, but if I try to access any of the entry's fields in the view, I get undefined method. I'm using Rails 3 (don't ask) and v1.0.0 of this Gem, here's my controller:

class ContentfulPagesController < ApplicationController
  before_filter :connect_to_contentful_api, :set_url

  def show
    @response = @contentful.entry(nil, :content_type => 'page',
                                       'fields.url'  => @url,
                                       :include      => 10,
                                       :limit        => 1)

    @page = @response.first
  end

  private

  def connect_to_contentful_api
    @contentful = Contentful::Client.new(
      :access_token    => 'foo',
      :dynamic_entries => :auto,
      :entry_mapping   => {
        'page' => ContentfulPage,
      },
      :space           => 'bar',
    )
  end

  def set_url
    @url = '/'
  end
end

The ContentfulPage model:

class ContentfulPage < Contentful::Entry
  # Currently empty
end

The Entry returned from the API:

#<ContentfulPage: @fields={:title=>"Home", :url=>"/"} @sys={ ... }>

And the view that's trying to output the title field:

<%= @page.title %>

No matter what I try, I'm always getting this error:

undefined method `title' for #<ContentfulPage:0x007f4652d21370>

I've seen a few tickets here with people suffering similar issues, but they all appear to have been going about it the wrong way, or there was a bug which has since been fixed. Am I also doing this wrong?

Other things I've tried:

I'm going round in circles and can't seem to figure this out. Any chance someone can help out please? :)

@dlitvakb
Copy link
Contributor

Hey @Jonic,

Contentful::Entry does not support field accessors, you have to either inherit from Contentful::DynamicEntry or avoid inheritance at all and use the built-in Contentful classes.

Unless you want to add custom behaviour, they should work out of the box.

In case of wanting custom behaviour, changing to inheriting Contentful::DynamicEntry should solve most of your problems.

Hope this helps

Cheers

@Jonic
Copy link
Author

Jonic commented Sep 30, 2016

@dlitvakb Hi David, thanks for that - unfortunately that's one of the things I've tried already, and I still couldn't get it to work!

What I've done instead is provided a list of fields for each class, and then during initialize I've looped through them and created dynamically named methods for each. That was the only thing I could think of to get it working for me!

If I've missed something obvious about inheriting from Contentful::DynamicEntry the please let me know, but for now I'm happy with this solution too :)

@dlitvakb dlitvakb mentioned this issue Jan 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants