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))
Related
I need to monkey-patch one of the Rails core classes, specifically ActionView::Helpers::UrlHelper::ClassMethods.link_to method. As far as I remember there are some events fired when parts of Rails are loaded, how to add handlers for them? Or should I just put the code into initializer?
link_to does not appear to be in ClassMethods. From here.
In config/initializers/url_helper_extensions.rb
module ActionView
module Helpers
module UrlHelper
alias_method :_link_to, :link_to
def link_to
# Your code ...
# Call original method if you want.
_link_to
end
end
end
end
i was wondering how do you make a helper file's functions available to an action mailer? i have an action mailer called UserMailer and a helper called sessions_helper.rb. how do i make the methods available to UserMailer? ive tried 'include' but it gives the following error
the method im trying to get is "current_user" and i receive the error
undefined local variable or method `cookies'
im using rails 3.2.1
thanks
You can helpers in your mailers like this,
class Notifier < ActionMailer::Base
add_template_helper(ApplicationHelper)
#...
end
As far as current_user go, I don't think mailers have any concept of cookies as ActionController does. As a better design choice I'd keep my mailer independent of the current_user. Mailers are not concerned with who the current_user is ( similar to models ).
For that matters, mailers are not even concerned with who the user is, they are concerned with "email, subject, and body".
So when calling mailer methods, you can pass them the user object ( it can be of the current_user or any body else ) so that they can get the email , generate the subject and the body.
I have had to add a file to config/initializers with this:
class ActionMailer::Base
helper MiscHelper
helper ExtraMailHelper
end
I guess you would add lines for other helpers as needed, e.g.:
class ActionMailer::Base
helper MiscHelper
helper ExtraMailHelper
helper SessionsHelper
end
If you want to include specific helpers in specific mailers you have, you can use this.
class RegistrationMailer < ActionMailer::Base
include MyOwnHelper
def method
end
end
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.
So I was trying to call link_to in a helper module
here is the code
module GroupsHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::UrlHelper
def self.link_to_publish
link_to "test"
end
end
It returns
undefined local variable or method `link_to' for GroupsHelper:Module
Does anyone know why? Thx
Drop the "self.". It's not a class method.
Assumption: GroupsHelper is in rails app's app/helpers/groups_helper.rb
I'm trying to call sanitize within a controller. Here's what I tried:
class FooController < ApplicationController
include ActionView::Helpers::SanitizeHelper
# ...
end
However, I'm getting this error:
undefined method `white_list_sanitizer' for FooController:Class
I searched around and people recommended switching the include line to include ActionView::Helpers, but that results in this error:
undefined method `url_for' for nil:NilClass
What's the correct way to call sanitize? I'm using Rails 2.3.5.
you can use this ActionController::Base.helpers inside action method:
class SiteController < ApplicationController
def index
render :text => ActionController::Base.helpers.sanitize('<b>bold</b>')
end
end
Hope this helps
Rails 6:
To strip links (for example) from a text, just call:
...
Rails::Html::LinkSanitizer.new.sanitize("links here will be stripped")
...
see https://github.com/rails/rails-html-sanitizer
I'm not sure what you're trying to do here but I'm almost 100% certain it doesn't belong in the controller.
If you want to sanitize an attribute before you save it to the DB, do so in the model with a before save callback.
Otherwise, sanitize in the view template or view helper.