I want to render dropdown list in form with images as options. How to achieve this in rails3?
I like the msdropdown. You can add the path for the image in the title tag:
<select>
<option value="1" title="#path for the image">first</option>
<option value="2" title="#path for the image">second</option>
...
</select>
In Rails, you can add the title doing something like:
<%= f.select(:yourcolumn, Model.all.map{|p| [p.name, p.id, :title => p.image_url(:thumb)]}
I have faced a similar problem some time ago: generate HTML <select> with title inside each <option> to apply the msDropDown plugin
Check out this plugin http://www.marghoobsuleman.com/jquery-image-dropdown there is no option to do it directly in Rails.
Related
I have this with simple form:
= simple_form_for(Note.new, remote: true) do |f|
= f.collection_select :dance_id, current_user.dances.includes(:style).all,
:id, :style_name, {prompt: "Please select dance"}, {class: "form-control"}
Code generated:
<select class="form-control" name="note[dance_id]" id="note_dance_id">
<option value="">Please select dance</option>
<option value="39">Adowa Dance (Ghana)</option>
<option value="38">Adowa Dance (Ghana)</option>
<option value="37">Tango (Argentina)</option>
</select>
which works fine as it is displaying all dances by style_name as i want it. But i want to make a difference between two dances and want to also show level and date right next to each dance in the collection because there are many dances with same name bc of the style which is the same!
please help. any better way to do it with simple form helpers?
As stated in the discussion, you needed to declare a method in your model's rb file like this:
def custom_name
"#{style.name}. #{date}"
end
And change the :style_name parameter to :custom_name
I'm building an Rails app were the user must select type of business via f.select.
I've used f.selectbefore without any problems. but now I have to add sub-categories to the select.
In my app the user have to select one category and then have to select a sub-category according to the parent category.
How would I do it? is it possible?
This is basically what I need:
---category 1----
--sub-cat-1a--
--sub-cat-1b--
--sub-cat-1c--
----Category 2 ----
--sub-cat-2a--
--sub-cat-2b--
--sub-cat-2c--
----Category 3 ----
--sub-cat-3a--
--sub-cat-3b--
--sub-cat-3c--
I'm not sure how I would manage to do this, any suggestion would be great.
I've been googling around without success
I haven't start building this yet, I wanted to check here first if someone had a solution to this
This can definitely be done. You can use the standard rails form or the simple_form gem to do this.
For example if you had categories and items to display, your standard rails form would look like this:
<%= form_for #item do |f| %>
<%= f.grouped_collection_select(:category_id, Category.order(:name),
:items, :name, :id, :title,
include_blank: "Please Choose...") %>
<% end %>
This displays the main categories, and then the subcategories (items in this case) in each category. Its output looks like this, in which accessories, canon camera etc are the main categories, and manfrotto tripod, canon 5D3 are the sub categories.:
It is similar to the html optgroup tag which looks like this:
<select>
<optgroup label="Group 1">
<option>Option 1.1</option>
</optgroup>
<optgroup label="Group 2">
<option>Option 2.1</option>
<option>Option 2.2</option>
</optgroup>
<optgroup label="Group 3" disabled>
<option>Option 3.1</option>
<option>Option 3.2</option>
<option>Option 3.3</option>
</optgroup>
</select>
Which produces:
Obviously you would have to modify this to fit your project, but it can definitely be achieved.
For more flexible adjustment you can use select2 https://select2.github.io
I have this right now:
<%= f.select :credit, (0..500) %>
This will result in this:
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
...
How would I add another option in that select that would be "All" and which value should be nil?
This will almost do what you want:
<%= f.select :credit, ((0..500).map {|i| [i,i] } << ["No limit",nil]) %>
select can take a number of formats for the list of options. One of them is an array of arrays, as given here. Each element in the outer array is a 2-element array, containing the displayed option text and the form value, in that order.
The map above turns (0..500) into an array like this, where the option displayed is identical to the form value. Then one last option is added.
Note that this will produce a value of "" (an empty string) for the parameter if "Unlimited" is selected - if you put a select field into a form and the form is submitted, the browser will send something for that form parameter, and there is no way to send nil as a form parameter explicitly. If you really wanted to you could use some clever javascript to get the browser to not send the parmeter at all, but that would be more work than simply adding:
param[:credit] == "" and param[:credit] = nil
to your controller action.
If I understand the question correctly, you can use options_for_select and prompt to do this a little more cleanly than what is shown in the selected answer:
<%= f.select :credit, options_for_select(0..500), { prompt: "No Limit" } %>
See the docs here:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
following is the code of my select tag
<%= select_tag "assignee#{cnt}", options_from_collection_for_select(#arr,tmp="id","name" , default_selection), html_options = { :onblur => "myblur(this);", :onChange=> "submit_selected(this);", :style=> "visibility:visible;" } %>
the html generated for above line is like:
<select id="assignee1" name="assignee1" onChange="submit_selected(this);" onblur="myblur(this);" style="visibility:visible;">
<option value="12">Name1</option>
<option value="48">Name2</option>
<option value="15">Name3</option>
<option value="35">Name4</option>
</select>
now in the default_selection paramter, i want to use the option value corresponding to option text. i have the option text.
say, i have "Name1" and i want to use its option value i.e 12 in default_selection parameter
any comments?
any comments?
Yes ;) Indeed your question is not related to this portion of code. You just have to search in your object #arr.
My bet :
default_opt = #arr.select {|o| o.name == 'Name1'}
default_selection = default_opt.id if default_opt
Then generate your <select> as you have already done.
I have a settings model with a column options, and set it to serialize with serialize :options. In my view, I have a multiple selection box, using select("settings", "options", ['option1','option2','option3'], {}, :multiple => true) which works fine so long as the user selects at least one option. However, if they don't select any options, no options are submitted and so the options aren't updated.
How do I allow the user to select zero options from a multiple selection box in rails?
That has nothing to do with rails: html form won't send such parameter to server if nothing is chosen in 'select' element. But you should be able to fix it in controller. Something like this
if params[:settings] == nil
params[:settings] = [];
end
Not sure if there's more elegant solution.
Add a hidden field after the select box, that posts an empty value to "settings[options]"
It's same trick that rails uses to make sure unchecked checkboxes get posted as false.
I do not like assuming a value is empty if the attribute is not posted. It breaks the way Rails expects to update attributes, and it can present problems if you are using your controller actions also for APIs as well as HTML. My preferred way of handling this is by adding a hidden input field before multiselects.
<input type="hidden" value="" name="parent_model[my_attribute_ids][]">
If you use JQuery you can automate the addition of these hidden input fields:
$('select[multiple="multiple"]').each(function(i){
$(this).before('<input type="hidden" name="'+this.name+'" value="" />')
});
I realize this answer is not very timely, but I hope this will help someone with a similar question.
You could provide a "None" option.
Example from: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}, {:multiple => true})
Would become
<select name="post[person_id]" multiple="multiple">
<option value="">None</option>
<option value="1">David</option>
<option value="2" selected="selected">Sam</option>
<option value="3">Tobias</option>
</select>