Multiple selected data doesn't appear - ruby-on-rails

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) : [] %>

Related

Formtastic/ActiveAdmin set a default value for a select input

I am working on a custom form in ActiveAdmin which I decided to use it for adding/editing to follow the DRY principle so I need to populate it if the user uses it for editing a record ( which isn't a DB record ).
So the problem is I have those inputs :
f.input :model_id, as: :select, collection: Model.all.map { |m| [m.id.to_s + ' - ' + m.name, m.id] }, input_html: { required: true }
f.input :enabled, as: :select, collection: {'Yes': true, 'No': false}, input_html: { required: true }
And I'd like to set a default value for them if I'm using the form for editing but I failed to know how because everybody is talking about using belongs_to or a DB relation and ActiveAdmin will take care of the default value for you which isn't applicable for my case because it's not a DB record and I don't have an ActiveRecord Model for it.
Even the official docs of Formtastic didn't help.
I figured out how to do it and here is the answer for anyone facing the same problem :
f.input :model_id, collection: Model.all.map { |m| [m.id.to_s + ' - ' + m.name, m.id] }, selected: object.model_id
f.input :enabled, collection: { 'Yes': true, 'No': false }, selected: object.enabled

Rails 4: Create JSON array in array inside input

For now, i have this inside my form:
<%= f.input :document, collection: #documents, wrapper: false, label: false, input_html: {class: 'fleft mleft5'} %>
which will production in json array:
[{
"document":"126"
}]
My Controller under create method:
document: params[:ng_geofence][:document]
My Model
def as_hash(format=:google)
{
document: document
}
end
%w[document].each do |key|
define_method(key) do
value && JSON.parse(value)[key]
end
end
my question is how to get the output for something like this:
[{
"document":
{
"id":"126"
}
}]
---- UPDATED ----
If i changed my form to:
<%= select_tag "ng_geofence[driver_notification][document][id]", options_from_collection_for_select(#documents, "id", "name"), include_blank: true %>
I get this error: 757: unexpected token at '{"document"=>{"id"=>"126"}}'
Probably because of "=>" instead of ":"
Solved my own problem. Here what i did:
My Controller under create method:
document: {
id: params[:ng_geofence][:document]
}
My Model
def as_hash(format=:google)
{
document: { id: document }
}

Can't specify a class with simple_form gem with a block

I have the following form but for some reason, I can't get the HTML class to come appear in the resulting code. The documentation suggests this should work, but they don't have an example that uses a block. I've tried a few different things but I haven't found the right answer yet. I have to use a block to get the custom data to appear with the select.
= simple_form_for([:admin, #theme]) do |f|
= f.input :title_bar
= f.input :apple_touch_icon_image, input_html: { class: 'imagepicker' } do
= f.select :apple_touch_icon_image_id, Theme::AppleTouchIconImage.where(company: #company).map{ |i| [i.id, i.id, { 'data-img-src' => i.image.url(:thumbnail) }] }
= f.button :submit, class: "btn-success"
I would try:
= f.input :apple_touch_icon_image do
= f.select :apple_touch_icon_image_id, Theme::AppleTouchIconImage.where(company: #company).map{ |i| [i.id, i.id, { 'data-img-src' => i.image.url(:thumbnail) }] }, {}, { class: 'imagepicker' }

Rails - Add custom item to select

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 } %>

Pass a parameter into collection select method for text_method

<%= collection_select(:catgory, :id, #categories, :id, :title, {}, data: { behavior: 'category_dropdown' }) %>
In the above code I need to pass a parameter to the title method. Is there any way to do this with collection_select?
<%= collection_select(:catgory, :id, #categories, :id, (:title, #program), {}, data: { behavior: 'category_dropdown' }) %>
Edit:
Looking at the internals for collection_select the text_method. It is eventually passed to a .send method which should allow for element.send(:title, #program). However, I think the issue why I still can't pass the param is that collection select is reading (:title, #program) as two params instead of one.
Use select instead:
select "catgory", "id", #categories.map{|c| [c.title(#program), c.id]}, {}, data: { behavior: 'category_dropdown' }
Should be working.
This can be done with collection_select if your model has an existing parameter you can overwrite:
f.collection_select( :your_model_id,
YourModel.all.map{|ym| ym.name = ym.custom_name(your_parameter); ym},
:id, :name,
{:selected => #model_instance.logic},
{:class => 'your class', :other => "..." } )
For instance I do this to conditionally pluralize my model's name attribute
class MyModel < ActiveRecord::Base
DEFAULT_NAME_COUNT = 99
def pluralized_name(n = DEFAULT_NAME_COUNT)
begin
return name.pluralize(n)
rescue
end
name
end
end

Resources