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.
Related
I keep getting this error when I try to load the index page:
NoMethodError in MessageController#new_message
undefined method `action' for MessageController:Class
The error goes away if I replace BaseController with ApplicationController, but that also breaks a lot of other things and the tutorial(http://websocket-rails.github.io/) for rails-websocket has it as BaseController.
class MessageController < WebsocketRails::BaseController
def initialize_session
# perform application setup here
controller_store[:message_count] = 0
end
def new_message
new_message = {:message => 'this is a message'}
send_message :new_message, new_message
end
def index
end
end
Does anyone know what the problem is here?
--
edit
using rails version 4.0.2
if that matters at all
Seems like this has been reported as an issue on the project's issue tracker in the past, but has been closed (with non-fix).
The comment explaining the situation:
You don't want to mix ApplicationController methods with ones from WebsocketRails::BaseController. In fact, WebsocketRails::BaseController is designed after ApplicationController and implements some of the same methods, namely process_action.
The solution I used (and which they also suggest), is to use 2 controllers. One inheriting from WebsocketRails::BaseController and another from ActionController::Base (Rails). Put your logic in a separate class and use it from both.
I'm trying to create clean controller based on ActionController::Base. That's what I try:
class MetalController
ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
include left
end
end
From Rails doc:
Shortcut helper that returns all the modules included in
ActionController::Base except the ones passed as arguments:
This gives better control over what you want to exclude and makes it
easier to create a bare controller class, instead of listing the modules
required manually.
My another controller inherits from MetalController :
class API::BaseController < MetalController
#.... my awesome api code
end
So this not work then i launch rails server:
block in <module:AssetPaths>': undefined methodconfig_accessor' for
MetalController:Class (NoMethodError)
Rails 4.1.0, Ruby 2.1.0
Update:
If i include ActiveSupport::Configurable
throws the errors:
_implied_layout_name': undefined local variable or method
controller_path' for MetalController:Class (NameError)
You need to inherit from ActionController::Metal:
class MetalController < ActionController::Metal
ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
include left
end
end
In my Rails application I have this:
class ProjectsController < ApplicationController
include ApplicationHelper
def index
#payments = current_user.projects.custom_order
end
...
end
module ApplicationHelper
def custom_order
order("name ASC")
end
end
However, in my index view I get this error:
undefined method 'custom_order' for #<Class:0x007f8be606ff80>
How can this be done?
I would like to keep the custom_order method in a helper module because I am using it across various different controllers.
Thanks for any help.
I know what you mean, but the right way is use model scopes:
class Project < ActiveRecord::Base
#...
scope :custom_order, order('name ASC')
#...
end
Move it to application controller.and make it a helper method.
Or include application helper module in application controller. So you will be able to access all application helper methods in all controllers.
Normally View Helpers are meant for rendering html based outputs, There you couldn't expect your model based queries.
Once you write a helper method in Application helper, It can be called in Any views.
Change your query like this
#payments = current_user.projects.order("name")
Or otherwise In your model Make it with scope.
scope :custom_order, order('name ASC')
In my Rails 3 application I use Ajax to get a formatted HTML:
$.get("/my/load_page?page=5", function(data) {
alert(data);
});
class MyController < ApplicationController
def load_page
render :js => get_page(params[:page].to_i)
end
end
get_page uses the content_tag method and should be available also in app/views/my/index.html.erb.
Since get_page uses many other methods, I encapsulated all the functionality in:
# lib/page_renderer.rb
module PageRenderer
...
def get_page
...
end
...
end
and included it like that:
# config/environment.rb
require 'page_renderer'
# app/controllers/my_controller.rb
class MyController < ApplicationController
include PageRenderer
helper_method :get_page
end
But, since the content_tag method isn't available in app/controllers/my_controller.rb, I got the following error:
undefined method `content_tag' for #<LoungeController:0x21486f0>
So, I tried to add:
module PageRenderer
include ActionView::Helpers::TagHelper
...
end
but then I got:
undefined method `output_buffer=' for #<LoungeController:0x21bded0>
What am I doing wrong ?
How would you solve this ?
To answer the proposed question, ActionView::Context defines the output_buffer method, to resolve the error simply include the relevant module:
module PageRenderer
include ActionView::Helpers::TagHelper
include ActionView::Context
...
end
Helpers are really view code and aren't supposed to be used in controllers, which explains why it's so hard to make it happen.
Another (IMHO, better) way to do this would be to build a view or partial with the HTML that you want wrapped around the params[:page].to_i. Then, in your controller, you can use render_to_string to populate :js in the main render at the end of the load_page method. Then you can get rid of all that other stuff and it'll be quite a bit cleaner.
Incidentally, helper_method does the opposite of what you're trying to do - it makes a controller method available in the views.
If you don't want to include all of the cruft from those two included modules, another option is to call content_tag via ActionController::Base.helpers. Here's some code I recently used to achieve this, also utilizing safe_join:
helpers = ActionController::Base.helpers
code_reactions = user_code_reactions.group_by(&:code_reaction).inject([]) do |arr, (code_reaction, code_reactions_array)|
arr << helpers.content_tag(:div, nil, class: "code_reaction_container") do
helpers.safe_join([
helpers.content_tag(:i, nil, class: "#{ code_reaction.fa_style } fa-#{ code_reaction.fa_icon }"),
helpers.content_tag(:div, "Hello", class: "default_reaction_description"),
])
end
end
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