WHAT AM I TRYING TO ACHIEVE
I have a html select box. The select box is responsible for selecting the preferred languages. for instance if a page is displayed in Germany then the select box should display Germany as its preference and likewise for the other languages.
WHAT HAVE BEEN DONE SO FAR
In application.html.erb I have below code
#application.html.erb
<select name="language" onChange="location = this.options[this.selectedIndex].value;">
<% @langs.each_pair do |short, long| %>
<option value="/<%= short %>/<%= @rem %>" <% if short==@langu %> selected="selected"<% end %>><%= long %></option>
<% end %>
</select>
In application_controller.rb I have below code
#application_controller.rb
@string = request.fullpath
@langu = @string.split("/")
@rem = @string.split("/#{@langu[1]}/")
@langu = @langu[1]
@rem = @rem[1]
@langs = { :en => 'english',
:es => 'espaniol',
:de => 'germany',
:fr => 'french',
:it => 'italy' }
WHAT IS NOT WORKING
When user views a website in English/Spanish/Germany OR selects from the list, the select box evidently should show the particular language. i.e. if English then english, if Spanish then espaniol and so on. This is the exact thing not working
WHAT IS WORKING
If the user selects the preferred language (lets say Spanish) from the select box it renders the specific page, however the select box displays english which in essence should be espaniol.
Thanks!
There’s just a minor typo in your string. It should be
selected='selected', so it should work like this: