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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:52:14+00:00 2026-06-13T22:52:14+00:00

I created the following helper method and it is working fine but I want

  • 0

I created the following helper method and it is working fine but I want to improve the way it is being called.

def display_if_present(attribute, i18key, hours=nil)
  unless attribute.blank?
    content_tag :li do

      if hours.present?
        concat content_tag :h2, get_translation_by_model_attr('achievment', i18key) + ' ('+t(:hours)+'): '
      else
        concat content_tag :h2, get_translation_by_model_attr('achievment', i18key)+': '
      end

      if i18key.include? 'date'
        concat content_tag :p, attribute.strftime('%m/%Y')
      elsif hours.present?
        concat content_tag :p, h(attribute) + "(" + t(:hours) + ")"
      else
        concat content_tag :p, h(attribute) 
      end
    end
  end
end

And on the view side I have the following:

<%= display_if_present(academic_achievment.institution,'institution') %>

<%= display_if_present(academic_achievment.ativs_description,'ativs_description') %>

<%= display_if_present(academic_achievment.date_start,'date_start') %>

<%= display_if_present(academic_achievment.date_finished,'date_finished') %>

<%= display_if_present(academic_achievment.load, 'load', :hours) %>

<%= display_if_present(academic_achievment.tags, 'tags') %>

which I want to do some refactor. So I tried:

<% elements = %w(institution ativs_description date_start date_finished load tags) %>

<% elements.collect! do |item| %>
  <% params = (item == "load") ? "item, :hours" : "item" %>
  <%= display_if_present("academic_achievment.#{item}".constantize, params.constantize) %>
<% end %>

the above block of code returned the error:

wrong constant name academic_achievment.institution
Extracted source (around line #15):

12: 
13:         <% elements.collect! do |item| %>
14:           <% params = (item == "load") ? "item, :hours" : "item" %>
15:           <%= display_if_present("academic_achievment.#{item}".constantize, params.constantize) %>
16:         <% end %>
17:  
18:       </ul>

I appreciate some help for a better piece of code.

  • 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-13T22:52:15+00:00Added an answer on June 13, 2026 at 10:52 pm

    I recommend using the send method like this:

    <% %w(institution ativs_description date_start date_finished load tags).each do |item| %>
      <%= display_if_present(academic_achievment.send(item), item, item == 'load' ? :hours : nil) %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following helper method: def orders_chart_series(orders, start_time) orders_by_day = orders.where(:created_at => start_time.to_date..Date.today). group(date(created_at)).
I created the following method for retrieving stored settings from the database: public String
i created the following sample method in business logic layer. my database doesn't allow
The following code is a helper I've created for a system which allows administrators
I created the following helper in my main directory under /Helpers: HtmlHelpers.cs using System;
I have created an NSManagedObject called TItem. To that object, I've added a helper
I have the following HtmlHelper method that I want to create a button that
I have created following rule in the .htaccess file located at the root of
I have created following thing in android using android compatibility support package Basically i
I need to have the URL like photos/13/rate => I have created following routing

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.