Showing posts with label helper. Show all posts
Showing posts with label helper. Show all posts

Monday, September 07, 2009

Helpers Outside Views

How to access helper methods outside of the view layer?


# models/category.rb
def description
  "This category has #{helpers.pluralize(products.count, 'product')}."
end

def helpers
  ActionController::Base.helpers
end

# products_controller.rb
def create
  @product = Product.new(params[:product])
  if @product.save
    flash[:notice] = "Successfully created #{@template.link_to('product', @product)}."
    redirect_to products_url
  else
    render :action => 'new'
  end
end


Reference: http://railscasts.com/episodes/132-helpers-outside-views