简体   繁体   English

如何在.html.erb页面上显示控制器的变量(循环中)? 红宝石在铁轨上

[英]how can I display controller's variable (which is on a loop) on .html.erb page? ruby on rails

I have the following code listed below in my controller: 我的控制器中列出了以下代码:

struc = {'en' => 'english', 'es' => 'espaniol', 'de' => 'germany', 'fr' => 'french', 'it' => 'italy'}
struc.each_pair do |key, value|
  @key=key
  @value=value
end

on my application.html.erb I have the following 在我的application.html.erb上,我有以下内容

<select name="Language" onchange="location=this.options[this.selectedIndex].value;">
    <option value="/<% @key %>/<%= @rem %>"><%= @value %></option>
</select>

Now how can i make the value of '@key' and '@value' appear recursively display on (application.html.erb)? 现在如何让(@key)和'@value'的值递归显示在(application.html.erb)上?

Also How can I change its styling ie make the value appear smaller, tucked in nicely?? 另外,如何更改样式,即使值看起来更小,隐藏得更好?

Thanks in advance 提前致谢

Why not something like 为什么不这样

in controller

@langs = { :en => 'english', 
           :es => 'espaniol', 
           :de => 'germany', 
           :fr => 'french', 
           :it => 'italy' }

in view

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

You will need to make struc available to the view by turning it into an instance variable - ie, @struc. 您需要通过将struc转换为实例变量-即@struc,使该视图可用。 Then in your view do 然后在您看来

<select name="Language" onchange="location=this.options[this.selectedIndex].value;">

<% @struc.each_pair do |key, value| %>
        <option value="/<%= key %>/<%= @rem %>"><%= value %></option>
<% end %>

</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Ruby on Rails html.erb 文件中循环 - Loop in Ruby on Rails html.erb file 如何在我的 html.erb 中调用 JS function 并将 ruby 变量作为参数传递? - How can I call a JS function within my html.erb and passing as argument a ruby variable? 我想为我的动态表使用一个控制器和html.erb文件。 我将如何在Ruby On Rails中做到这一点? - I want to use one controller and html.erb files for my dynamic table. How I will do it in Ruby On Rails? Ruby on rails:在Html.erb中使用&lt;%=%&gt;标记中的javascript变量 - Ruby on rails: using javascript variable in <%= %> tags in Html.erb 如何在Rails html.erb文件的ruby if / else循环中执行一些jQuery? - How do you do some jQuery inside a ruby if/else loop in a Rails html.erb file? 如何在Rails中使用link_to在控制器中加载html.erb页面 - How to use link_to in rails to load html.erb page in controller 未创建Ruby on Rails .html.erb文件 - Ruby on Rails .html.erb file not created Ruby on Rails-在html.erb中使用帮助器 - ruby on rails - use a helper in html.erb Ruby on Rails:html.erb中的值比较 - Ruby on Rails: comparison of values in html.erb 如果我有 2 个控制器,如何正确配置 .css 文件与 .html.erb 文件的连接。 Ruby on Rails - How to properly configure the connection of .css files to .html.erb files if i have 2 controllers. Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM