I have an activeadmin form, in which one of the inputs fields is populated by json data returned by some method as
f.input :tag, :label => 'Tags', :as => :select, :collection => HelperClass.get_json()
The json looks like this:
{
"group_name": "Group1",
"categories": [
{
"category_name": "cat_1",
"score": "120"
},
{
"category_name": "cat_2",
"score": "120"
}
]
}
While this is shown on the form in the UI, I want only the 'group_name' to appear. However, I would want to use the data in 'categories' later on.
Is there anyway I could do this? For eg, hide the remaining json from the form, or parse the json in some other place using 'group_name', or any other way that I can't think of..
PS: Could you also elaborate while you answer this. I am not a ROR developer, but had to modify the code written by someone else.
First convert to array/hash then use Ruby Array/Hash methods. Assuming you meant the JSON is an array of groups:
data = JSON.parse HelperClass.get_json()
f.input :tag, :label => 'Tags', :as => :select,
:collection => data.map {|grp| grp['group_name']}
I suspect you want a multi-level select, which will require you to write your own JavaScript to populate the second select. Maybe https://github.com/blocknotes/activeadmin_selectize will help with that.
Related
I am trying to set up a group of radio buttons in rails using simple_form. The options should come from a map.
In simple form, I saw there the :collection symbol can retrieve a collection (but only an array of arrays and not a hash).
How can I display a radio button collections with custom labels (ideally from a hash)?
Here is what I tried so far. Can I concatenate the symbol with an a string for a label?
#Payment.rb
OPTIONS = [[14, 19], [21,29], [30,39]] #ideally this would be a hash
#new.html.erb
<%= f.input :duration,
:collection => [ [14, 19.to_s], [21,29.to_s], [30,39.to_s] ],
:label_method => :last.concat(" days"),
:value_method => :first,
:as => :radio %>
if I understand your question correctly - you can use lambdas in :label_method. For example:
:label_method => lambda { |a| a.first.to_s + a.last.to_s }
I currently have a multiple select box in a rails form that looks like this:
= select_tag :in_all_tags, options_from_collection_for_select(Tag.where(:project_id => #project.id), :id, :name, #in_all_tags_param), { :id => "tags", :tabindex => "3", "data-placeholder" => "Choose Tags", :multiple => "multiple" }
Where
#in_all_tags_param = params[:in_all_tags]
The problem is, #in_all_tags_param will only populate the select form with the last value from params[:in_all_tags]. So, if the url string reads in_all_tags=5&in_all_tags=8, the pre-selected value in the multiple select will only be 8.
From what I understand, the way around this is to append [] to the field name for multiple params, so that :in_all_tags becomes in_all_tags[]
BUT, when I try this, submitting the form returns:
Expected type :default in params[:in_all_tags], got Array
Any suggestions appreciated.
Cheers...
You need to add a :name element to the same hash with :multiple => true in it. So I use something similar for Genres on an app for mine and I do { :multiple => true, :name => "lesson[genre_ids][]" }. The name has to be model[attribute][].
current i am trying to restrict the information feed into an option select field to only display the criteria i have selected. with the code below this seems to be working
= select("schedule", :selected_players, User.where(:team_id => current_user[:team_id]) { |p| [full_name(p), p.id] }, {:include_blank => 'None', :prompt => 'Add Players to Lineup'}, :multiple => "multiple")
the issue is that this code is display an array field type i.e #<User:0xa559830>
how do i get it to display the actual users name?
Define .to_s method in model
Like here
https://github.com/roolo/mwstt/blob/master/app/models/project.rb#L7
Also all the mapping and searching logic should be placed in model as method which you'll just call in view, or prepare it in controller!
<%= select_tag(:services,
options_from_collection_for_select(Service.all, :id, :name))%>
And it displays all the services...
But I want it to be something like:
Select a service
Service1
Service2
Service3
Service4
Most of the time, you don't want to append anything to the array directly; either of these is a cleaner solution:
Use :prompt => "Placeholder" if you want the placeholder to show up only when the attribute is nil at the time the form is rendered. It will be selected by default, but nothing will be saved if user submits. If the attribute is already populated [possibly because a) there's a default value or b) it's an edit form], the placeholder item will be omitted from the list entirely.
Use :include_blank => "Placeholder" if you want to include the placeholder in the rendered list at all times.
<%= select_tag(:services,
Service.all.collect { |c| [c.id, c.name] }.
insert(0, "Select a Service"))%>
As answered for the question, this is for Rails 2.3. For Rails 3, see Prathan Thananart's answer.
The better way to do this would be to use the :prompt parameter. Something like:
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
(Using Rails 2.3.5 on an internal work server with no choice of versions, and I'm pretty new)
I'm building a search form where I need to provide a list of directories to a user so they can select which one(s) to search against. I'm trying to figure out how to get the selected values of a collection_select to remain after the form is submitted.
Say the user selected 3 directories from the collection_select, the id's of those directories would look like this in the params:
directory: !map:HashWithIndifferentAccess
id:
- "2"
- "4"
- "6"
I know that you can manually specify multiple selected items:
<%= collection_select :directory, :id, #directories, :id, :name,
{:selected => [2,4,6]}, {:size => 5, :multiple => true} %>
I've also played around a bit and was able to us "to_i" against a single value in the params hash:
<%= collection_select :directory, :id, #directories, :id, :name,
{:selected => params[:directory][:id][0].to_i}, {:size => 5, :multiple => true} %>
What I can't figure out is how to use all of the values of the :directory params at the same time so what the user selected remains after the form is submitted. Thanks for any help.
I'm not precisely sure what you're asking, but if you're trying to get the array of strings in params[:directory][:id] as an array of integers, all you need is
params[:directory][:id].map{|id|id.to_i}