Reputation: 6124
While rendering xml for an object, I am getting the error
NoMethodError (undefined method `model_name' for OrderResponse:Class):
OrderResponse.rb
class OrderResponse
include ActiveModel::Serialization
attr_accessor :payload
end
In controller
def create
@order_response = OrderResponse.new
@order_response.payload = 12345
respond_to do |format|
format.xml { render :xml => @order_response }
end
end
I found other questions with similar titles while searching, according to that i modified 'respond_to' with 'respond_with' which inturns throws an error
undefinedMethod 'model_name' in OrderResponse
How to solve this?
Upvotes: 0
Views: 1867
Reputation: 2807
I found an answer to this somewhere on stackoverflow and wish I could credit the source... This is my interpretation of it.
In Rails 3, if you have a resource listed in your routes which has a model .rb file but no active record table behind it, then you'll see this kind of error. This appeared for me as a form_for trying to reference a :controller and :action in this model. Perhaps it is related to Rails attempting to process associations for the model or something similar. Either way, this is new for me since I upgraded an application from Rails 2.3.8.
For me, the appears as:
undefined method `model_name' for SomeModel:Class
To fix it, at the top of the affected class add:
extend ActiveModel::Naming
include ActiveModel::Conversion
def persisted?
false
end
This has worked for me on two models like this.
Upvotes: 3
Reputation: 9014
try including ActiveSupport::CoreExtensions::Module
where model_name is defined ActiveSupport::CoreExtensions::Module
Upvotes: 0
Reputation: 3542
You could try defining a class method by that name, which returns the name of the class:
def self.model_name; 'OrderResponse'; end
Upvotes: 1