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 7035273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:17:06+00:00 2026-05-28T01:17:06+00:00

In a Rails (erb) view, I want to conditionaly display some text and a

  • 0

In a Rails (erb) view, I want to conditionaly display some text and a link.

Is there anything simpler than that:

<% if some_condition %>
        Go to  
<%=     link_to("Home", root_url) %>
<%  else %>
        See all
<%=     link_to("objects", my_objects_path) %>
<%  end %>

I am a bit annoyed by the number of <% and <%= tags.

  • 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-05-28T01:17:06+00:00Added an answer on May 28, 2026 at 1:17 am

    As a helper (depending on actual usage, it could be an application-wide helper, a model helper, etc):

    def home_or_objects c
      if c
        link_to "Go to home", root_url
      else
        link_to "See all objects", my_objects_path
      end
    end
    

    (I wouldn’t linkinate only a single word–smaller target, and IMO less communicative. YMMV.)

    Then in the view:

    <%= home_or_objects some_condition %>
    

    As a partial it’s a matter of rendering the partial, perhaps passing the condition in as a local, or if it’s an instance variable, it can be referenced directly without needing to be passed.

    <%= render :partial => 'home_or_objects', :locals => { :c => some_condition } %>
    

    (There are a few ways a partial could be handled. IMO a helper is more appropriate.)

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

Sidebar

Related Questions

it seems that on Rails or erb, the only way to output anything is
In my Rails plugin, i want to display multiple view files onto a single
In my rails view(index.html.erb), i have following structure <div> <%= render :partial => create
(Warning: Clueless Rails Newbie!) In my show.html.erb for my albums view, I call a
I want to update an erb.html page in rails with information from a webscraping
In Rails, I want to run the js.erb file associated with an action on
I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so
I'm trying to add a link to a Rails view to set a boolean
I like how Rails gives me flexibility in naming view files, ie index.erb or
I've got some javascript that needs to load a rails partial - I'd prefer

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.