How to use JavaScript selected functionality when particular language is selected in conjunction with ruby?

↘锁芯ラ 提交于 2019-12-10 11:29:30

问题


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!


回答1:


There's just a minor typo in your string. It should be selected='selected', so it should work like this:

<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>



回答2:


BACKGROUND & SOLUTION

OK Guys! after spending and banging my head around for not being able to find a solution to trivial matter. I finally managed to work my way around to solve the bugs. In simple words, changes were needed in application_controller.rb where my data-set values of languages (of countries) were in Hash format which actually was required to be in a Array format (look above to see the question for hash format). Along with that I had to make a slight changes on application.html.erb See below for working source code

CHANGES MADE TO MAKE IT WORK

  • changed the Hash format to Array Format
  • changed each.pair to only each

IN application_controller.rb

@langs = [['en','english'],
          ['es','espaniol'],
          ['de','germany'],
          ['fr','french'],
          ['it','italy']] 

IN application.html.erb

<select name="language" onChange="location = this.options[this.selectedIndex].value;">    
    <% @langs.each do |short, long| %>
        <option value="/<%= short %>/<%= @rem %>" <% if short==@langu %> selected="selected" <% end %>><%= long %></option>
    <% end %>
</select>

NOTE Thank You to all, for your efforts to solve this bug. Hope this helps to someone in future.



来源:https://stackoverflow.com/questions/13497177/how-to-use-javascript-selected-functionality-when-particular-language-is-selecte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!