how to use data-attribute in rails3? - ruby-on-rails-3

I have a html select syntax like :
<select id="salary_structure_salary_structure_line_items_attributes_0_payhead_id" class="half" name="salary_structure[salary_structure_line_items_attributes][0][payhead_id]">
<option data-payhead="Earnings" value="2"> Basic </option>
<option data-payhead="Earnings" value="3"> Daily allowance </option>
<option data-payhead="Standard deductions" value="4"> Employee provident fund </option>
</select>
How do i get this work in rails syntax? what change should i do in below code to get the above result:
<%= select_tag "salary_structure[salary_structure_line_items_attributes][#{index}][payhead_id]", options_from_collection_for_select(#payheads, :id, :payhead_name, :selected => salary_structure_line_item.payhead_id ), :include_blank => true, :class => "half"%>
also i want to use 'selected' option in this code.

Hey I think this is rather complicated using the options from collection for select
See: http://www.redguava.com.au/2011/03/rails-3-select-list-items-with-custom-attributes/

options_array = [
["Basic", 2, "data-payhead" => "Earnings"],
["Daily allowance", 3, "data-payhead" => "Earnings"],
["Standard deductions", 4, "data-payhead" => "Standard deductions"]
]
select_tag("salary_structure[salary_structure_line_items_attributes][0][payhead_id]", options_for_select(options_array, value), :class => "half")
In the above code, in the options_for_select method please specify the value with the select box option's value. this will embed the selected attribute for that option

Related

Get value from multiple select dropdown on Yii to be inserted to database

Help I want to get value from a multiple select dropdown from a form to a controller.
Here's my form:
<select multiple="multiple" id="form-field-select-4" class="form-control search-select" name="tim_teknis">
<option value=""> </option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
</select>
And here's my controller:
$tim_teknis = $_POST['tim_teknis'];
It turned out to be showed like this: "tim_teknis" not the value of the dropdown. I hope anyone could understand what I mean. Thank you!
You have to implode the values before insert
May be your form not method is 'POST' , so "tim_teknis" not the value of the dropdown.
Solution:
<form action="submit_form.php" method="POST"><select multiple="multiple" id="form-field-select-4" class="form-control search-select" name="tim_teknis[]"><option value=""> </option><option value="AL">Alabama</option><option value="AK">Alaska</option></select></form><?php if (!empty($_POST['tim_teknis'])) {var_dump($_POST['tim_teknis']);}?>
Result after submit form:
array(2) { [0]=> string(2) "AL" [1]=> string(2) "AK" }

Compare current row with previous row of ActiverRecord result in Ruby on Rails

I need make an option list with optgroup from the results of a query like this:
<select>
<optgroup label="here one of the results of query">
<option>rest of results</option>
</optgroup>
</select>
I need something like this (or better). Example in php:
<?php
$sql= "query here";
$result=$connection->createCommand($sql)->queryAll(); //to execute the query and save in the variable result
for ($i=1; $i <count($result) ; $i++)
{
$a = $i-1
if ($result[$a]["optiongroup"] == $result[$i]["optiongroup"])
{
// agree to the optiongroup with an option
}
else
{
// close the optiongroup and starts a new option group
}
}
?>
I have this:
Controller:
#componentes = Tipo.find_by_sql("SELECT t1.tip_titulo as pieza, t2.tip_id, t2.tip_titulo FROM `tipos` t1
LEFT JOIN tipos t2 ON (t1.tip_id = t2.tip_id_padre)
WHERE t1.tip_id_padre IN (38)")
In the query the results show like this:
pieza tip_id tip_titulo
Fuente de poder 41 Ventilador
Fuente de poder 42 Conector de alimentacion
Memoria Ram 43 Placa
Memoria Ram 44 Reloj
So the select must appear in this way:
<select>
<optgroup label="Fuente de poder">
<option value="41">Ventilador</option>
<option value="42">Conector de alimentacion</option>
</optgroup>
<optgroup label="Memoria RAM">
<option value="43">Placa</option>
<option value="44">Reloj</option>
</optgroup>
</select>
How do I solve this with Rails 5.0.1?
greetings.
In a regular rails app you should define a view template(that should have the name of your controller with html.erb ending, for example products.html.erb)
Then you define in products.html.erb:
<select>
<optgroup label="here one of the results of query">
<% #components.each do |component| %>
<option value='<%= component.tip_id %>'> <%= component.tip_titulo %></option>
<%end%>
</select>
So as you will see that the #componenets you defined in the controller is autometically accessible in your template view.
See the docs for Rails select http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
I have exactly that #Joel_Blum, look:
<select class="form-control" name="tip_id_componente[]" id="tip_id_componente" multiple="multiple" size="8" style="color: black; width: 400px; ">
<% #componentes.each do |c| %>
<option id="<%= c.tip_id %>" value="<%= c.tip_id %>"><%= c.tip_titulo %> (<%= c.pieza %>)</option>
<% end %>
</select>
but it appears without the optgroup, if i agree the optgroup like this:
<select class="form-control" name="tip_id_componente[]" id="tip_id_componente" multiple="multiple" size="8" style="color: black; width: 400px; ">
<% #componentes.each do |c| %>
<optgroup label="<%=c.pieza%>">
<option id="<%= c.tip_id %>" value="<%= c.tip_id %>"><%= c.tip_titulo %> (<%= c.pieza %>)</option>
</optgroup>
<% end %>
Then show the html code like this:
<select>
<optgroup label="Fuente de poder">
<option value="41">Ventilador</option>
</optgroup>
<optgroup label="Fuente de poder">
<option value="42">COnector de alimentacion</option>
</optgroup>
<optgroup label="Memoria ram">
<option value="43">placa</option>
</optgroup>
<optgroup label="Memoria ram">
<option value="44">reloj</option>
</optgroup>

how to insert three dropdown valuein one field in to database

How can i insert three dropdown values in one field into my database for inserting date of birth. I am $dob variable is it ok or not? please help me.
My form is
<form name="" id="order-form" class="smart-form" role="form" action="" method="post" enctype="multipart/form-data">
<tr>
<td><font color="#334D66"> Date of Birth: </td>
<td><select name="birthday">
<option selected></option>
<option>01</option>
<option>02</option>
.
.
</select>
<select name="birthmonth">
<option selected></option>
<option>January</option>
<option>February</option>
.
.
</select>
<select name="birthyear">
<option selected></option>
<option>1999</option>
<option>1998</option>
.
.
</select>
</td>
</tr>
AND
my insert code like this
<?php
if(isset($_POST['submit']))
{
$day=$_POST['birthday'];
$month=$_POST['birthmonth'];
$year=$_POST['birthyear'];
$dob = $day.'/'.$month.'/'.$year;
$data =array('username' => $_POST['username'],
'firstname' => $_POST['firstname'],
'lastname' => $_POST['lastname'],
'email' => $_POST['email'],
'homepage' => $_POST['homepage'],
'adress' => $_POST['adress'],
'city' => $_POST['city'],
'province_id' => $_POST['province_id'],
'postcode' => $_POST['postcode'],
'country_id' => $_POST['country_id'],
'phone' => $_POST['phone'],
'dob' => $dob,
'gender' => $_POST['gender'],
'occupation' => $_POST['occupation'],
'fav_quotes' => $_POST['fav_quotes'],
'news' => $_POST['news'],
'newsletter' => $_POST['newsletter'],
'reg_type_id' => $reg_type_id,
'images' => $_POST['images']
);
insert('user', $data);
//echo "Success";
header("Location: paypal-invoice/createandsend.example.php");
}
?>
Thanks In Advance.
you can insert dob in a mysql dababase because mysql accept yyyy-mm-dd format so you should
$year.'-'.$month.'-'.$day
I think it will work for you

Haml implement select multiple as in HTML

<select multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
How in Haml can I create a select multiple tag like in HTML? Like the nice helper function of select_tag.
Just do this
%select{:multiple => true}
%option{value: 'volvo} Volvo
%option{value: 'saab'} Saab
%option(value: 'opel'} Opel
%option{value: 'audi'} Audi
You can obviously also use select_tag:
= select_tag
EDIT
An even cleaner way to do the above:
%select{:multiple => true}
- ["Volvo", "Saab", "Opel", "Audi"].each do |o|
%option{value: o.downcase} o

Pluralize for collection

I need a help
I'm using simple form for select box. The code is here: = f.input :dogs, collection: (0..7). When render on browser, it will display select box with value from 0 to 7. I want add text behind value when select box dropdown. Eg: 1 dog - 2 dogs. I tried but not work.
Pls help me
You can use something like the following:
= f.input :dogs, options_for_select([["1 dog", 1], ["2 dogs", 1]])
= f.input :dogs, options_for_select((1..8).map{|x| x==1? ["#{x} dog",x] : ["#{x} dogs",x] })
A really simple and elegant solution could be something like this:
Create a helper method:
def humanize_dogs
0.upto(7).each_with_object({}) { |c, h| h[pluralize(c, 'dog')] = c }
end
# => {"0 dogs"=>0, "1 dog"=>1, "2 dogs"=>2, "3 dogs"=>3, "4 dogs"=>4, "5 dogs"=>5, "6 dogs"=>6, "7 dogs"=>7}
In your form add the next line:
= f.input :dogs, collection: humanize_dogs
This generates...
<select id="#" name="#">
<option value=""></option>
<option value="0">0 dogs</option>
<option value="1">1 dog</option>
<option value="2">2 dogs</option>
<option value="3">3 dogs</option>
<option value="4">4 dogs</option>
<option value="5">5 dogs</option>
<option value="6">6 dogs</option>
<option value="7">7 dogs</option>
</select>