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

The problem is fixed, because I'm using a redirect, the values of the variables are cleared. Now I'm using sessions and that's working great.

Adam thanks for your time and help!

I've added the before filter, but it still doens't work. Any other suggestions?

I freely admit, I didn't test that code in any way, I just wrote it to give a general idea as I was brushing my teeth.

From what I see of your code, you don't have the before_filter, so the @current_language instance variable never gets set to anything. I suspect that's why your view is coming up blank.

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.

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

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.

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.

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:

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?

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

rather than this:

13 Posts
Login to add your message