can someone please help in select helper, as I am new to Ruby On rails.
I have one json object as follow,
@response = {"status" => "200", "fruit" => [ {
"id" => 1,
"name" => "Apple"
}, {
"id" => 2,
"name" => "Mango"
} ]
}
for which in helper I am returning value "return @response['rateCard']" Now what I want from this helper to generate such code in view file so it will have selection box like
<select name='fruit'>
<option value="1">apple</option>
<option value="2" selected>mango</option>
</select>
please help
Actually I have one helper "Fruits_helper.rb" which is returning
def selFruit
@resp = Fruit.getFruit # this will return @response object as mentioned above
return @resp['fruit']
end
开发者_运维百科
================================================================================== So in my fruit.html.erb file I am having small piece of code as follows
<%= form_for(:AdminLogin) do |f| %>
<div>
<%= select_tag "fruit", options_for_select(selFruit.each{|fruit,key| [fruit[key]=>'name',fruit[key]=>'id']}) %>
<div>
<% end %>
============================================================================ the above code gives me o/p as
<select name="fruit" id="fruit">
<option value="id1nameApple">id1nameApple</option>
<option value="id2nameMango">id2nameMango</option>
</select>
where I want result as apple
My Problem has been solved just by following code, hope it will help other RoR freshers too
<%= select_tag "fruit", options_for_select(selFruit.map{|fruit,key| [fruit['name'], fruit['id']]}) %>
If you use (and you should) a form_for
builder:
form.select(:fruit, @response["fruit"].map { |f| [f["name"], f["id"]] })
精彩评论