How to use auto_link inside of a controller - ruby-on-rails

In my controller I need to build a JSON object. How can I use auto_link() inside my controller? Right now it errors:
NoMethodError (undefined method `mail_to' for #<ConversationsController:0x144c3f880>):
app/helpers/application_helper.rb:48:in `html_format'
app/controllers/conversations_controller.rb:326:in `comments'
app/controllers/conversations_controller.rb:322:in `comments'
Thank you for any ideas

auto_link is an helper and you can only reference if from a view. And there's a reason for that: representing information is a view responsibility.
In your case, either create a JSON template or, if you really really really need to use the helper in the controller, include the ActionView::Helpers::TextHelper module in your controller.
class ConversationController < ApplicationController
include ActionView::Helpers::TextHelper
include ActionView::Helpers::UrlHelper
end
You might also need to include al dependencies, such as the module that contains mail_to helpers.

Currently, the auto_link function is removed from rails 3.1 you can use the rails_autolink gem instead.
https://github.com/tenderlove/rails_autolink

Related

Calling controller method from a helper module

Using Rails 3.2
I'm trying to call expire_fragment, the Rails view method, from a helper but getting an error:
undefined method `expire_fragment' for #<#<Class:0x00000118977110>:0x00000103b853b8>
I'm trying to conditionally clear the cache. This is the helper method call in my view
clear_cache_keys_if(params[:cc], [#product, :search_filters])
And in the helper
def clear_cache_keys_if(condition, keys = [])
if condition
keys.each do |key|
expire_fragment(key)
end
end
end
I would have thought that the Rails fragment caching methods would be accessible in a helper module, but that doesn't seem to be the case.
I changed it to
controller.expire_fragment(key)
And that worked.
This method is available in the view. It's available from the controller. I don't really understand why it is not available in a helper. What am I missing here? Why isn't it available in the helper and what is the best way to expose it? Thanks

How to include rails ApplicationHelper module in a regular Ruby Class

I have a class in my Rails app's lib directory and I want to be able to use the helpers I created in ApplicationHelper from inside that class. I have the following:
module ApplicationHelper
def works
"Yay!"
end
def breaks
session[:username]
end
end
class MyClass
include ApplicationHelper
end
The following works
MyClass.new.works
This breaks
MyClass.new.breaks
I get a message about Rails trying to delegate session to controller.session, but it can't do it because controller is nil.
What's the right way to include ApplicationHelper?
The issue is not with including the helper, but with accessing the session.
In the normal scenario, the ApplicationHelper is automatically included in all controllers, and some of the ActionView methods - including session - are delegated to the controller which is present in this case.
When included in a regular ruby class, there is no controller; so any call to these methods will throw an error saying the controller is nil.
Not sure why you would need session information in a regular ruby class.

Include Helper Method to a Controller

I'm new to rails and I have this small problem about helpers and controllers. Is it possible to include my custom helper to a controller?
Let's say I have this helper class.
Module UsersHelper
def my_helper
...
end
end
Then I have this controller.
class UsersController < ApplicationController
end
Can I use the my_helper in my controller?
Yes - There are several ways, not least including (mixing-in) the helper into the controller, or using the loophole explained here
But... if it's hard to do, or results in ugly code, it may not be the right way to do it. Can the method that's in the helper not be moved into the controller, and then delegated back to the helper using "helper_method"?
Yes. you can do by using include, but I don't recommend you doing this. Because Rails constructs under MVC architecture, you can learn more about MVC before include UserHelper in your controllers.
MVC Reference:
http://guides.rubyonrails.org/getting_started.html#the-mvc-architecture
Model–view–controller

how can i use a helper in different views

i am using refinery cms at the moment. I created an engine and with it some helpers in app/helpers/admin/.
now i would like to use those helpers in my frontend view (ie. app/views/myapp/index) as well. but i can not...undefined methode error.
what do i have to do short of copying the whole thing to app/helpers/?
the helper looks like this
module Admin
module myHelper
def somefunc
end
end
end
so is it possible to use somefunc outside of the Admin module?
The "Rails way" to include a helper from a non-standard path in a view is to use the .helper method within your controller.
class MyController < ApplicationController
helper Admin::MyHelper
...
end
http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper
In your application_helper.rb:
module ApplicationHelper
include Admin::MyHelper
end
This will import those helper methods into the ApplicationHelper, thus making them available in your views. You could do this in any of your helpers really.
You might try to use the full object reference like Admin::myHelper::somefunc to call somefunc from outside the Admin module.

Using sanitize within a Rails controller

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.

Resources