Question

I'm using ActiveResource 4.0 to map a RESTFull API to model classes in my Rails 4 Application. Im getting the error "undefined method 'model_name' for SimpleDelegator:Class" only when trying to edit an object.

It seems that objects loaded by ActiveResource's descendants are been created as SimpleDelegator's descendants instead - that can't respond to model_name method.

I did some reading on ActiveResource code but I couldn't understand why this is happening. Has anybody stumbled upon something like this?

My Model:

class Database < ActiveResource::Base
  self.size = "api.endpoint"
end

And in my controller (snippet)

def new
  @database = Database.new
end

def edit
  @database = Database.find(params[:id])
  if !@database
    redirect_to databases_path
  end
end 

On my form views (snippet):

<%= form_for @database do |d| %>

  <%= d.label :dbname %> <!-- error reported on this line -->
  <%= @database.dbname %>

  <%= d.label :description %>
  <%= @database.description %>

<% end %>
Was it helpful?

Solution

Ok, I found the problem. I'm also using the activeresource-response gem to get pagination params sent with HTTP headers.

As can be seen here This gem uses SimpleDelegator to wrap the response after the find method execution to "capture" the full ActiveResource connection's htt_response.

I'll try to fix this in the gem or use another solution, but I think this answer can help someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top