How to Use an Actionview::Helper in a Ruby Script, Outside of Rails

How do I use an ActionView::Helper in a Ruby script, outside of Rails?

~> irb
ruby-1.9.2-p180 :001 > require 'action_view'
=> true
ruby-1.9.2-p180 :002 > ActionView::Base.new.number_to_currency 43
=> "$43.00"

Rails using ActionView::Helper methods outside of view

If you're in the console, you should be able to just do helper.simple_format('hi'). The helper method is available in console as a way to call some helper methods.

When using a custom helper:

# app/helpers/custom_helper.rb
module CustomHelper
def custom_method(x)
puts "Custom method #{x}"
end
end

# from the console
helper.custom_method('hi')

# from the controller
class SomeController < ApplicationController

def index
view_context.custom_method('hi')
end
end

Using Rails' ActionView Helpers outside of Rails

You can try bringing in enough of the various Rails gems to support it (starting with ActionPack and continuing with ActiveSupport), but you're definitely going to have to bring in quite a lot - it eventually needs to have a locale system initialized with locale files for translation.

dvyjones suggestion will be much quicker if you don't need that localization.

How to properly include ActionView::Helpers in the class outside of ActionController::Base?

I figured out how to do this. The error was caused by incorrect url_for invocation, and it should be called with parameter only_path, like so:

def create_text(popularities = build_list)
text = 'Those users interested in your contacts: '
popularities.map! do |reciever|
link_to reciever.full_name, url_for([reciever, only_path: true])
end
text + popularities.join(', ')
end

Or as alternative a default_host parameter should be set in the enviropment configuration file.

Ruby using helpers in single script



require 'action_view'
include ActionView::Helpers::TextHelper
puts pluralize(2, 'person')

(Requires gem actionpack installed, tested with ruby 1.9.1, actionpack 3.0.3)

How to include ActionView helpers in the assets pipeline?

Create a inititializer and include the helpers in the context of the assets like this:

Rails.application.assets.context_class.class_eval do
include ActionView::Helpers
include MyAppHelper
include Rails.application.routes.url_helpers
end

Taken from this sprockets issue

Include ActionController::Base outside of views directory

It is not working as you have your angularview template in assets directory(as you have mentioned in your question: root/app/assets/templates/angularview.html.erb). You need to create it inside your application's app/views/layouts/ directory.

See these answers for more information:

  1. https://stackoverflow.com/a/6951986/645886
  2. https://stackoverflow.com/a/19849989/645886

UPDATE: However, if you must do that then you can create an initializer and put this code:

Rails.application.assets.context_class.class_eval do
include ActionView::Helpers
include MyAppHelper
include Rails.application.routes.url_helpers
end

Source: https://stackoverflow.com/a/14284279/645886



Related Topics



Leave a reply



Submit