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"]] })
Related
I am new to ruby on rails please help me with this simple code :
I need to print
'HI' if option 1 is selected
'BONJOUR' if option 2,
'HOLA' if option 3,
'NAMASTE' if option 4.
on selecting a value on the dropdown and click the button,
<%= form_for :person do |f| %>
<%= f.select :desired_attribute, ['option1','option2','option3','option4']%>
<%= button_to "Show Text" , :onclick => "show_back(), return false" %>
<% end %>
involve your server.
view. Add remote: true to your form. And handle ajax request in controller. more about ajax in rails
<%= form_for :person, remote: true do |f| %>
<%= f.select :desired_attribute, options_for_select([ ['option1', '1'],['option2', '2'],['option3', '3'],['option4', '4']]) %>
<%= button_to "Show Text" , :onclick => "show_back(), return false" %>
<% end %>
in your controller
class PersonsController
MY_HASH = { '1': 'HI', '2': 'BONJOUR', '3': 'HOLA', '4': 'NAMASTE' }
def name_of_action
respond_to do |format|
format.js { render 'your_partial_name', name: MY_HASH[params[:person][:desired_attribute]] }
end
end
your_partial_name.js.erb
alert("<%= name %>");
Instead of MY_HASH it's better to use I18n to handle language issues. more about I18n
Use this syntax:
<%= f.select :point1, options_for_select([ ['option1', 'HI'],['option2', 'BONJOUR'],['option3', 'HOLA'],['option4', 'NAMASTE']]) %>
you will dropdown like this:
<select id="point1">
<option value="HI">option1</option>
<option value="BONJOUR">option2</option>
<option value="HOLA">option3</option>
<option value="NAMASTE">option4</option>
</select>
Dropdown option map with its value then you will get the value.
I have this rails code :
<% status_a = [ ["DRAFT", "DRAFT"], ["OPEN", "OPEN"], ["CLOSE", "CLOSE"] ] %>
<%= form_for(:dash_action, url: brokers_dashboard_path ) do |f| %>
<%= f.select(:select_status, options_for_select(status_a), {}, selected:'OPEN' %>
<% end %>
When it runs, it generates this HTML code :
<select selected="selected" name="dash_action[select_status]" id="dash_action_select_status">
<option value="DRAFT">DRAFT</option>
<option value="OPEN">OPEN</option>
<option value="CLOSE">CLOSE</option>
...
But what I expect is :
selected="OPEN" and not "selected"
Why the select method is not doing what I want ?
Try following code snippet, default value should be the parameter of options_for_select
f.select :select_status, options_for_select(status_a, 'OPEN')
I am using bellow code and function options_from_collection_for_select for generating options for user.
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'email') %>
Above code generate bellow html:
<select id="receiver" name="receiver" style="display: none;">
<option value="1">email1#yahoo.com</option>
<option value="2">email2#gmail.com</option>
<option value="3">email3#gmail.com</option>
</select>
But i want email with username, e.g <optionvalue="1">email1#yahoo.com(some_user)</option>
Suggestion any alternate function or customize current function will be appreciated.
In your User model add a method :
def user_dispay_name
"#{email}(#{full_name})"
end
Now do :
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'user_dispay_name') %>
But without options_from_collection_for_select this function i'm using below code:
<%= select_tag 'receiver', options_for_select(#user.map{ |c| ["#{c.email} (#{c.display_name})", "#{c.id}"] }) %>
I have a select_tag in ruby on rails. The syntax for this is,
<%= select_tag "iso_region", options_for_select(#all_regions.collect {|p| [ "#{p['cc']}-#{p['lr']}", p['cc'] ] }), class: "form-control selectpicker reg_name", :data => {:'live-search' => 'true'} %>
Sample Options generated is like this,
<option value="ET">ET-Africa</option>
<option value="NG">NG-Africa</option>
<option value="PG">PG-Pacific</option>
<option value="IT">IT-Europe</option>
And I want IT-Europe to be selected in the dropdown.
How can I do that with my select_tag?
Try this:
options_for_select(#all_regions.collect {|p| [ "#{p['cc']}-#{p['lr']}", p['cc'] ] }, "IT"),
The last arg you pass to options_for_select is the value to be marked selected when it's rendered.
I have an action that renders a view which contains this:
<select id ='dynamic_select'>
<option value = "<%= activity_path %>">All</option>
<option value = "<%= activity_path(:type => 'enrolled') %>">Enrolled</option>
<option value = "<%= activity_path(:type => 'redeem') %>">Redeem</option>
<option value = "<%= activity_path(:type => 'social') %>">Social</option>
</select>
What would be the correct/Rails way of rendering that select and mark as selected one of the options depending on the type parameter:
If there is no type parameter, select "All" options, if there is type=enrolled parameter select Enrolled option, and so on...
I have managed to do that client side with Javascript, but I am wondering what would be the Rails way of doing so.
Rendered HTML:
<select id="dynamic_select" name="dynamic_select">
<option value="/activity">All</option>
<option value="/activity?type=enrolled">Enrolled</option>
<option value="/activity?type=redeem">Redeem</option>
<option value="/activity?type=social">Social</option>
</select>
Something like the following. I have displayed the select_options here, but you should probably generate them in your controller and pass them through to the view.
The key is using options_for_select.
<% select_options = {"All" => activity_path} %>
<% %w{Enrolled Redeem Social}.each {|opt| select_options[opt] = activity_path(:type => opt.downcase)} %>
<% form_for(resource) do |f| %>
<%= s.select :dynamic, options_for_select(select_option, :selected => select_options[#default || "All"]) %>
<% end %>