I have a select box like this, it works and is saved in the database, but after I refresh the page the selected value doesn't appear as selected. Any suggestions?
.col-md-6
%span.label.label-purple{:style => "pointer-events: none; font-size: 14px"} Country Default
= select_tag :country_default, options_for_select(Location.where(loc_type: "country").map { |country| [country.name, country.loc_code] }),
style: "margin-top: 11px;",
include_blank: "",
class: "country-default select2", data: { select: "select2"}
options_for_select has an optional second argument, which will display your selected option, if one has been selected.
...options_for_select(Location.where(loc_type: "country").map { |country|
[country.name,
country.loc_code]}, #YOURMODEL.country_default)...
Use collection_select instead of select_tag like this:
=collection_select :country, :default, Location.where(loc_type: "country"), :loc_code, :name,
{include_blank: '', selected: 'select2' },
{class: 'country-default select2', style: 'margin-top: 11px;'}
another option is using select to generate select boxes.
See the Action View Helpers Guide
Related
I'm wondering what is the best solution to individually update an associated record with simple_fields_for.
I have texts associated to an order. So this what I have in my view:
= f.simple_fields_for(:texts) do |ff|
.card
.card-body
.order-text
= ff.input( \
:content,
label: false,
required: false,
disabled: !#order.finalized?,
input_html: { \
class: 'form-control order-text-field',
rows: 6,
placeholder: '' })
- if #order.finalized?
br
= ff.link_to_remove( \
'Delete this text',
data: { confirm: "Do you really want to delete this text?" })
I want to add a button under each text (like the link_to_remove) to perform a specific action + update the record.
For example, under the 2nd text, I would like to edit it, then click on a button to update ONLY this record (so I have to get the params of this specific text) + perform an action.
How can I achieve this?
I use select2 for multiple select. My problem is, I can select multiple value from json data and I can save this data in related model columns. But when I want to update this model, the selected data doesn't appear.
some part of my json data:
{
- categories:[
- {
id:7,
name:"Eğitim Bilimleri Enstitüsü"
},
- {
id: 8,
name: "Eğitim Yönetim Teftişi ve Planlaması 1. Yarıyıl"
},
...
]
}
I have a Moodle class(it is not inheritance from ActiveRecord, just a class and it has some functions which return json data). I am wondering whether it is right approach.
in the content of my form:
<%= simple_form_for(#period) do |f| %>
...
<%= f.input :moodle_connect_ids, collection: Moodle.categories.map {|p| [ p['name'], p['id'] ] }, input_html: {multiple: true} %>
...
<% end %>
I explicitly set moodle_connect_ids to be an array in my controller:
params.require(:period).permit(..., :moodle_connect_ids => [])
in the content of my .js file:
$("#period_moodle_connect_ids").select2({
placeholder: "Moodle Dönem Bağlantılarını Seçiniz",
multiple: true,
allowClear: true
});
When I select to multiple values and save my model, the column's value looks like this:
> Period.last
=> #<Period:0x007fcf53a4d830
id: 25,
...
moodle_connect_ids: "[\"\", \"85\", \"120\"]"
...
Am I on the wrong way? Have you any suggestion?
I've figured out how to do this issue.
I've added selected option in the f.input:
selected: #period.moodle_connect_ids? ? JSON.parse(#period.moodle_connect_ids).map(&:to_i) : []
the input's last state:
<%= f.input :moodle_connect_ids, collection: Moodle.categories.map {|p| [ p['name'], p['id'] ] }, input_html: {multiple: true}, selected: #period.moodle_connect_ids? ? #period.moodle_connect_ids.map(&:to_i) : [] %>
I have a select helper as seen below, I have disabled it and would like give "1" as default value, but couldnt find how?
<%= f.select :fuel_incl, [[t('shared.incl'),'1'],[t('shared.notInc'),'0']], {}, {class: "Select-control u-sizeFull fuel_incl", :disabled => true} %>
It is disabled so it should select 1 as default.
EDIT
I see that it works from the console;
<select class="Select-control u-sizeFull fuel_incl" disabled="disabled" name="boat[fuel_incl]" id="boat_fuel_incl">
<option selected="selected" value="1">Included</option>
<option value="0">Not Included</option></select>
but it does not send as params, when I take out disable true and select value send the form, it works..
EDIT2
anyways, I have solved it by giving only one select value as;
<%= f.select :fuel_incl,
[[t('shared.incl'),'1']],
{ selected: '1' },
{ class: "Select-control u-sizeFull fuel_incl" } %>
Add selected option:
<%= f.select :fuel_incl,
[[t('shared.incl'),'1'],[t('shared.notInc'),'0']],
{ selected: '1' },
{ class: "Select-control u-sizeFull fuel_incl", disabled: true } %>
I have this line of code:
<%= select "family", "num_of_children", (1..10), { :include_blank => true } %>
I would like to add one item at the end of the list. In this case: I want a "more" option as the last item.
How can I do that?
Convert the range to an array, then append 'more' to it:
<%= select "family", "num_of_children", (1..10).to_a << 'more', { :include_blank => true } %>
<%= select "family", "num_of_children", (1..10).to_a + [['more', '11']], { :include_blank => true } %>
Using Haml 3.1.4 (Separated Sally)
I am curious as to what I am doing wrong.
Why does this not show the first radio button selected?
btw, at execution, #organization.pdf_size does equal 'letter_size'
I would actually like the radio button selected based on the
#organization.pdf_size, however I am just trying to get a hard
coded selection to work atm. tyfyt
= form_for [#organization] do |f|
Select a PDF page size
= label_tag 'Letter (8.5x11)'
= f.radio_button :pdf_size, id: 'letter_size', :checked => true
= label_tag 'Half Legal (8.5x7)'
= f.radio_button :pdf_size, id: 'half_legal_size'
= f.submit 'Save', class: 'button'
I have also tried other examples I have seen on stackoverflow, in this fashion:
= f.radio_button :pdf_size, id: 'letter_size', :checked => #organization.pdf_size == 'letter_size' ? true : nil
Try this:
= f.radio_button :pdf_size, "value", id: 'letter_size', :checked => true
As documented here, the radio button needs a value before the options.
Remember to change "value" to something that makes sense for your application.
Try appending this to your link:
input_html: {checked: true}