Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:23:52+00:00 2026-06-09T14:23:52+00:00

Viget Labs posted an article and gist detailing a rails helper method for adding

  • 0

Viget Labs posted an article and gist detailing a rails helper method for adding a particular class (like .selected or .active) to a navigation link if it’s url matches the current path.

You can use it in your layout like so:

= nav_link "News", articles_path, {class: "btn btn-small"}

<!-- which creates the following html -->

<a href="/articles" class="btn btn-small selected">News</a>

Nice. I’m using bootstrap, and want to have an icon in my button, so I need to generate the following html:

<a href="/articles" class="btn btn-small selected"><i class="icon-home"> </i> News</a>

I forked the gist and figured out a simple way of doing it. My fork lets the developer pass :inner_html and :inner_class to the helper like so:

= nav_link "News", articles_path, {class: "btn btn-small"}, {inner_html: 'i', inner_class: 'icon-home'}

It works fine, but I don’t like my underlying implementation:

def link
  if @options[:inner_html]
    link_to(@path, html_options) do
      content_tag(@options[:inner_html], '', :class => @options[:inner_class]) + " #{@title}"
    end
  else
    link_to(@title, @path, html_options)
  end
end

As you can see, I’m passing the new options to content_tag inside the block of a link_to method. I was hoping I would be able to refactor it in a few ways.

First of all, I’d prefer to be able to do this in my view:

= nav_link "News", articles_path, {class: "btn btn-small"} do
  %i.icon-home

I want to give the inner html as a block, and not as attributes of the option hash. Can anyone give me any pointers on how to achieve this?

I thought it would a simple case of telling the nav_link method to accept a block:

def nav_link(title, path, html_options = {}, options = {}, &block)
  LinkGenerator.new(request, title, path, html_options, options, &block).to_html
end

class LinkGenerator
  include ActionView::Helpers::UrlHelper
  include ActionView::Context

  def initialize(request, title, path, html_options = {}, options = {}, &block)
    @request      = request
    @title        = title
    @path         = path
    @html_options = html_options
    @options      = options
    @block        = block
  end

  def link
    if @block.present?
      link_to @path, html_options do
        @block.call
        @title
      end
    end
  end

But this fails to output the icon, and instead inserts a number (4). I don’t get it clearly. Anyone got any advice. Where can I go to read more about this sort of thing, as I really want to be able to figure stuff like this out without having to ask on stackoverflow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:23:53+00:00Added an answer on June 9, 2026 at 2:23 pm

    The solution in the end was as follows:

    # capture the output of the block, early on if block_given?
    def nav_link(title, path, html_options = {}, options = {}, &block)
      LinkGenerator.new(request, title, path, html_options, options, (capture(&block) if block_given?)).to_html
    end
    

    I also had to modify my link method:

    def link
      if @block.present?
        link_to(@path, html_options) do
          @block.concat(@title)
        end
      else
        link_to(@title, @path, html_options)
      end
    end
    

    I’ve updated my gist. You could probably hack it up to accept more complex blocks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does ASP.NET MVC have anything like the RESTful out-of-the-box features like Ruby on Rails
I've got a fancy-schmancy worksheet style view in a Rails app that is taking
Using garb (http://github.com/vigetlabs/garb) i need to fetch the same Map Overlay XML, like Google
I would like to create (or use, if one already exists) a command-line based
How I can add external control like click event to any anchor out side
I would like to know your opinion about javascript template engine, which one you
i have a page post2.php which prints the json array like { page: 1,
Is is possible to get more photos from flickr. What is the standard /
I am working on iphone application in which i have to insert multiples values
I have a user control I use multiple times on a page. It uniformly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.