hap*_*elp 1 ruby select ruby-on-rails helper
有人可以帮助选择帮手,因为我是Ruby On rails的新手.
我有一个json对象如下,
@response = {"status" => "200", "fruit" => [ {
"id" => 1,
"name" => "Apple"
}, {
"id" => 2,
"name" => "Mango"
} ]
}
Run Code Online (Sandbox Code Playgroud)
在帮助器中我返回值"return @response ['rateCard']"现在我想从这个帮助器在视图文件中生成这样的代码所以它将有选择框像
<select name='fruit'>
<option value="1">apple</option>
<option value="2" selected>mango</option>
</select>
Run Code Online (Sandbox Code Playgroud)
实际上我有一个帮助者"Fruits_helper.rb"正在返回
def selFruit
@resp = Fruit.getFruit # this will return @response object as mentioned above
return @resp['fruit']
end
Run Code Online (Sandbox Code Playgroud)
================================================== ================================所以在我的fruit.html.erb文件中我有一小段代码如下
<%= form_for(:AdminLogin) do |f| %>
<div>
<%= select_tag "fruit", options_for_select(selFruit.each{|fruit,key| [fruit[key]=>'name',fruit[key]=>'id']}) %>
<div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
================================================== ==========================上面的代码给了我o/p as
<select name="fruit" id="fruit">
<option value="id1nameApple">id1nameApple</option>
<option value="id2nameMango">id2nameMango</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在哪里我想要结果作为苹果
我的问题只是通过以下代码解决,希望它也能帮助其他RoR新手
<%= select_tag "fruit", options_for_select(selFruit.map{|fruit,key| [fruit['name'], fruit['id']]}) %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3905 次 |
| 最近记录: |