I'm returning to RoR after not using it for a few years and I'm trying to use ActiveModel to serialise a plain object to XML.
I'm doing the following, as per the comments in activemodel/lib/activemodel/serialization.rb:
class XmlError
include ActiveModel::Serializers::Xml
attr_accessor :code
attr_accessor :description
def attributes
#attributes ||= {'code' => 'nil', 'description' => 'nil'}
end
def initialize(error_code)
#code = error_code
#description = "blah"
self
end
end
I use this in a controller as:
render :xml => XmlError.new("invalid_login")
and I get the following stacktrace:
NoMethodError (undefined method `model_name' for XmlError:Class):
app/controllers/users_controller.rb:19:in `login'
app/controllers/users_controller.rb:5:in `login'
If create a model_name class method, I then get the following stacktrace:
NoMethodError (undefined method `element' for "XmlError":String):
app/controllers/users_controller.rb:19:in `login'
app/controllers/users_controller.rb:5:in `login'
It feels like I'm chasing my tail here. Have I just missed something simple in my class? I followed the example closely.
extend ActiveModel::Naming
is what you are looking for.
http://rdoc.info/github/lifo/docrails/master/ActiveModel/Naming
Why not sub-class ActiveModel::Base?
Related
What's the best way to handle this type of error? As the mode requires a valid_object, but when it does not get it, it correctly provides validation error for the missing attribute, but then is raises a nil object error on valid_car_type?.
This is just a simplified example.
Model
class Car
include ActiveModel::Model
validates_presence_of :subject :valid_object
validate :valid_car_type
def valid_car_type?
valid_object.car_type == "truck"
end
end
NoMethodError:
undefined method `valid_car_type?' for nil:NilClass
Use active record call back for example 'before_save, after_save, etc...' and call the valid_car_type?
class Car
include ActiveModel::Model
validates_presence_of :subject :valid_object
before_save :valid_car_type?
def valid_car_type?
valid_object.car_type == "truck"
end
end
I'm new to Rails and I'm having a hard time understanding why my helper module doesn't work when called from the ActionMailer. I'm calling the same method from a different partial and it works fine. The problem is not so much the method but my session variable (session[:geo]) - it says "undefined method `session'".
here is my code any suggestion is much appreciated
products_helper.rb
def isUserLocal?
session[:geo] #true or false
end
def itemTotalPrice(item)
if self.isUserLocal?
item.line_item_us_total_price
else
item.line_item_w_total_price
end
end
order-notifier - ActionMailer
class OrderNotifier < ActionMailer::Base
helper :Products #helpers are not available in ActionMailers by default
received.html.erb
<%= render #order.line_items -%>
_line_items.html.erb
number_to_currency(itemTotalPrice(line_item))
I have this error
ArgumentError (wrong number of arguments (1 for 0)):
lib/law/production.rb:20:in `clone'
lib/law/production.rb:20:in `clone_law'
lib/law/production.rb:11:in `initialize'
app/controllers/laws_controller.rb:86:in `new'
app/controllers/laws_controller.rb:86:in `prod_law'
app/controllers/laws_controller.rb:44:in `create'
when using this
module Law
class Production
attr_accessor :law
attr_accessor :creator
def initialize(law,current_user)
#law = law
#creator = current_user
clone_law
end
def current__user
User.find_by_authentication_token(session[:_csrf_token])
end
def clone_law
clone(#law)
end
end
end
where clone, create, prod_law are some methods
I assume Rails is expecting a hash but I don't understand why
Firstly, clone is a standard Ruby method.
Secondly, it expects no arguments at
all (as error message says), it should be called on the object you want to clone, like this:
#law.clone
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?
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.
You could try defining a class method by that name, which returns the name of the class:
def self.model_name; 'OrderResponse'; end
try including ActiveSupport::CoreExtensions::Module where model_name is defined ActiveSupport::CoreExtensions::Module
I have a Rails app in which I am rendering a block of Haml stuff stored in a model attribute. It would be nice to use Rails view helpers in that block of Haml. Currently I am using the Haml::Engine#render in a view helper to render the content of this model attribute. It works well enough but I can't use things like =link_to. To illustrate the problem:
irb(main):003:0> haml_text=<<"EOH"
irb(main):004:0" %p
irb(main):005:0" =image_tag 'someimage'
irb(main):006:0" EOH
=> "%p\n =image_tag 'someimage'\n"
irb(main):007:0> engine = Haml::Engine.new(haml_text)
=> #<Haml::Engine:0x7fa9ff7f1150 ... >
irb(main):008:0> engine.render
NoMethodError: undefined method `image_tag' for #<Object:0x7fa9ff7e9a40>
from (haml):2:in `render'
from /usr/lib/ruby/gems/1.8/gems/haml-3.0.25/lib/haml/engine.rb:178:in `render'
from /usr/lib/ruby/gems/1.8/gems/haml-3.0.25/lib/haml/engine.rb:178:in `instance_eval'
from /usr/lib/ruby/gems/1.8/gems/haml-3.0.25/lib/haml/engine.rb:178:in `render'
from (irb):8
Any thoughts on how to do that?
Better ideas?
The render method allows you to specify a context. Something like
base = Class.new do
include ActionView::Helpers::AssetTagHelper
include ApplicationHelper
end.new
Haml::Engine.new(src).render(base)
could work.
Marcel was going in the right direction. But you need to get a valid scope for the render engine from somewhere. What I did was call the helper with a valid scope like this:
In my_view/edit.html.haml
=my_revertable_field(self, 'hello world')
In application_helper.rb
def my_revertable_field(haml_scope, title, field)
template =<<EOS
.field
#{label}
= text_field_tag #{field.name}, #{field.amount}, :size=>5, :class=>"text"
= image_tag("refreshArrow.gif",:class=>"revert-icon", :style=>"display:none;",:title=>"Revert to default, #{field.default}")
EOS
end
Then you have a valid haml scope and so image_tab, form_tag_helpers all work
class RailsRenderingContext
def self.create(controller)
view_context = ApplicationController.helpers
class << view_context; include Rails.application.routes.url_helpers; end
view_context.request = controller.request
view_context.view_paths = controller.view_paths
view_context.controller = controller
view_context
end
end
class MyController < ApplicationController
def show
# ...
engine = Haml::Engine.new haml
ctx = RailsRenderingContext.create(self)
engine.render ctx
end
end
It works for me. Based on this issue.