Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Create dynamic ids for header tags
13 Posts
Create dynamic ids for header tags

Hi,

I'm creating a website with some header tags and I'd like to create dynamic id's so I can use it as an anchor. This site is created in 2 languages, dutch and french, so I use I18N.

To do this, I've created 2 methodes in my model called short_dutch and short_french. I'd like to create the name of this methode dynamicly. (so I could use this with I18N) (see part 1)

If I do this staticly (see part 2), it works perfect, but if I change this to (part 3), it doesn't work anymore.

Could someone tell me what I'm doing wrong?

The code can be found at this url : http://pastie.org/495525

Unless #short_dutch and #short_french are actually columns in the database you can't access them using the index operator. You likely want this:

rather than this:

I generally find it's best not to use the index operator on ActiveRecords unless you're doing so for a very specific reason. Doing so skips any type casting, method overloads, etc.

Hi Adam,

The fields in my db are title_dutch and title_french. What would you suggest doing to create an anchor for a header tag?

Sorry, the markdown ate some of my response because of the HTML. I meant to say you likely want this:

rather than this:

Thanks Adam for your help, your solution did work.

I have 1 more question, is it better to use an tag rather than a content_tag(:h3.....)? Or doesn't it matter?

As you might expect, #content_tag will generate a tag that contains content, while #tag will only generate closed tags. In this case, your h3 tag contains the title string, so you'd use #content_tag like this:

content_tag(:h3, content.send("title#{I18n.locale}"), :id => content.send("short#{I18n.locale}"))

If you were to use #tag you would only get a closed h3 like this:

I just looked at your question again here on the site, rather than in the RSS feed, and it looks like you fell victim to the same markdown + HTML problem that messed up my original response. Based on that I see your question doesn't actually regard #tag vs. #content_tag, but using <h3> directly in the ERB vs. generating it with #content_tag. Right?

The answer to that question is, as you said, it doesn't matter. If you want to pull the functionality into a helper then using #content_tag is easier. If not, just writing the markup is fine.

Thanks for your help.

I've got 1 more question regarding I18n : On my site, i have a link to change the language from French to Dutch or from Dutch to French with this code : http://paste.pocoo.org/show/120296/.

But when I click the link the first time, nothing happens, when I click it a second time it works.

I also use a before filter to check whether the value of the cookie is dutch of french.

You're storing the current locale in two different ways, in cookies[:lang] and in i18n.locale. You check the latter in the view when determining which link to render, but you set the new locale based on the former in the controller. I'd guess the first time you render the page they're somehow out of sync.

I'd recommend fixing this type of error by making sure they're always the same, probably in a before_filter on your ApplicationController, and then always using one or the other in all of your code. Something like:

# In application_conroller.rb

attr_reader :current_language
helper_method :current_language
before_filter :set_current_language

def current_language=(new_language)
  self.current_language = i18n.locale = cookies[:lang] = new_language
end

private

def set_current_language
  self.current_language = i18n.locale = cookies[:lang]
  redirect_to :controller => :splash unless valid_current_language?
end

def valid_current_language?
  %w(french dutch).include?(current_language) # or whatever is appropriate for your app
end

This allows you to use the #current_language and #current_language= helper methods in your controllers and views, and makes sure your cookie stays in sync with the value of i18n.locale

There are some problems with your code so I tried to rebuild it piece by piece.

http://paste.pocoo.org/show/120420/

But my view doesn't return anything. Do I have to create an attr_reader :current_language ? The methode is public.

13 Posts
Login to add your message