Simple form input as dropdown not select box - ruby-on-rails-3

I have a input with a collection that should display as a drop down but instead it's displaying as a select box. How do i make it a drop down?
<%= f.input :fund, collection: funds, prompt: 'Select Fund', label: false, input_html: { multiple: true } %>

Add as: :select to your HTML and remove the multiple: true attribute. The select tag as a dropdown list does not support multiple selections without a JavaScript library like Select2.
<%= f.input :fund, as: :select, collection: funds, prompt: 'Select Fund', label: false %>
Here is an example of each type of select: http://jsfiddle.net/scarver2/s1nckfq5/
CITE: https://github.com/plataformatec/simple_form#usage

I answered a similar question here but you don't say what context (web framework) you're using.
You can use SimpleForm, Bootstrap and Bootstrap-Select. There is a Rails Gem for it. See my other answer for details.

Related

ActiveAdmin Autocomplete dropdown select not working

I am using activeadmin 0.4.4 along with rails3-jquery-autocomplete 1.0.15.
I was able to add autocomplete to a field.The dropdown shows perfectly as required.
However I am not able to select a value from the dropdown. (If I manually write the value in the text field it works fine). I get this error in the browser console and I am unable to figure out on how to proceed with this:
Uncaught TypeError: undefined is not a function
t.railsAutocomplete.fn.extend.init.t.autocomplete.select -- active_adimn.js line 13982
Please suggest.
Just installing activeadmin_addons gem you can convert select controls into select2 controls. Use active admin's DSL as normal:
form do |f|
f.inputs 'Detalles' do
f.input :brand
# more inputs...
end
end
to get this:
If you want the normal select control, add default-select class like this:
form do |f|
f.inputs 'Detalles' do
f.input :brand, input_html: { class: 'default-select' }
# more inputs...
end
end
to get this:

How to add html link for a simple_form field in rails 3.2?

Here is the quote_task field in simple form.
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => #quote_task.id}, :readonly => true %>
Now we want to add an embeded html link for the #quote_task.id to show the content of the quote_task. When a user click the id, he will be directed to the content of the quote task. Something like:
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => (link_to #quote_task.id.to_s, quote_task_path(#quote_task.id))}, :readonly => true %>
Is there a way to do this in simple_form? Thanks for help.
your expectation for HTML are way beyond any possible semantic.
http://www.w3schools.com/tags/tag_input.asp
http://www.w3.org/html/wg/drafts/html/master/forms.html#the-input-element
Yes it is possible that when one click on a input, it will show desired content. However without JS this wont be possible
input:
<%= f.input :quote_task, :label => t('Quote Task'), :readonly => true, class="redirecter", :'data-quote-task-path'=>quote_task_path(#quote_task.id) %>
coffee script using jQuery:
#app/assets/javascript/my_input.coffee
jQuery ->
$('input.redirecter').click ->
path = $(this).data('quote-task-path')
document.write(path); # ugly redirect, use Ajax
simple solution, but better would be if you load some content from server to your page with Ajax http://api.jquery.com/jQuery.ajax/
but my opinion is that you shouldn't do this at all. Inputs are for forms, forms are for submitting data. What you should be really using is pure link_to without any input due to HTML semantics. If you want it to look like input that you can style it to look like input, point is don't rape input tag for what it not meant to do.
it's not possible to embed anchors within input fields.
you can use javascript to do whatever magic that field should have.
if you want to find out more about javascript. go to amazon, buy a book, read it.

Is there a way to pass params when clicking submit button in simple_form view in rails 3.2.12?

In simple_form view, the submit button is like this:
<%= f.button :submit, 'Save' %>
We are trying to pass a params subaction when clicking the Save button. The params[:subaction] should have value of 'update' after clicking the button. Here is what we tried in view but it did not work:
<%= f.button :submit, 'Save', :subaction => 'update' %>
Is there a way to pass a value in params[:subaction] when clicking the Save button?
Use name and value option.
<%= f.button :submit , name: "subaction",value: "update"%>
In your controller you will get params[:subaction] with the value "update"
as dax points out,
<%= hidden_field_tag(:subaction, 'update') %>
<%= f.button :submit, 'Save' %>
This will provide the string value 'update' to the routed controller action via the hidden field. It can then be retrieved by the controller with
params[:subaction]
By specifying f.button :button, type: 'submit', we can use the name and value attributes as follows to submit using a single param. Notably, the value submitted (e.g., 'cake') may be different from the button text (e.g., 'The Best Cake').
_form.html.erb
<%= f.button :button, 'The Best Cake', type: 'submit', name: 'dessert_choice', value: 'cake' %>
<%= f.button :button, 'The Best Pie', type: 'submit', name: 'dessert_choice', value: 'pie' %>
Controller
def controller_action
dessert_choice = params[:dessert_choice] # 'cake' or 'pie'
end
This approach avoids the need for hidden inputs as #dax mentioned in the comment above.
Tested on Simple Form 3.3.1 with Rails 4.2.

simple_form error messages do not go away

I'm using simple_form with twitter bootstrap on Rails.
Everything works great, except when showing live validations in a form-inline class. My code for the form is:
<%= simple_form_for #message,
url: mailing_list_path,
html: { class: "form-inline" },
method: :post,
validate: true do |f| %>
<%= f.input_field :email_address, label: false %>
<%= f.submit "Submit" %>
<% end %>
This shows the error message properly (e.g. "is invalid"), but if I click off the input and then back on again, it adds another message (e.g. it would say "is invalid is invalid"). For example, two sequential invalid entries and then a blank entry would give:
Is there any way to have simple_form remove the existing error message before adding a new one?
EDIT:
I solved this using some ghetto js, but would still like to know if the functionality I mentioned above is built in. The divs are still there, they're just hidden instead of all showing together. Would be great to have them actually removed by the form validation...
$('input.email-address-input').on 'keyup', () ->
$(this).parent('form').siblings('.help-inline').hide()

Rails 3: f.select - options_for_select

I have a form on my Ruby on Rails3 Application with a drop menu, this is my current code for the select option:
<%= f.select :phone_type, options_for_select(["Select One", "Cell", "Work", "Office", "Home", "Other"],:disabled => ["Select One"]), :class => 'genForm_dropBox' %>
From my understanding this should have "Select One" as the default option when someone opens the page, but if they don't select one of the other options an error displays when they hit submit.
This is true in Browsers like Safari and Chrome and IE7, but in Firefox and IE8 it shows "Cell" as the first option as Select One is disabled.
I'd like it to display "Select One" by default, but have it as an unusable option when they submit the form. Do I need to script this into the controller, or model? or do I have this coded in the form wrong?
for those looking to incorporate this feature, I've taken a new approach from the model end of things. Being that all fields are required to be filled out in order for the user to submit and not receive an error alert, I gave the "Submit One" option a default value of nothing. You can take a look at the following code to see how I did that.
<%= f.select :phone_type, options_for_select([["Select One", ""], "Cell", "Work", "Office", "Home", "Other"]), :class => 'genForm_dropBox' %>
This is a little cleaner:
<%= f.select :phone_type, [ 'Cell', 'Work', 'Office', 'Home', 'Other' ], :prompt => 'Select One' %>
The :prompt argument generates an option with an empty value.
In Rails 4, this approach works well for me.
<%= f.select :status, options_for_status, prompt: 'Select One' %>
Meanwhile I have defined the options in a helper to keep the clutter out of my view.
def options_for_status
[
['First Option','first_option'],
['Second Option','second_option']
]
end
Thanks to everyone who contributed an answer.
I needed similar code for a project I'm working on and I really liked the approach Ryan Burnette took.
This is what worked for me using Rails 4.1.0.
<%= f.select :season, options_for_seasons, :prompt => 'Select One' %>
Then I defined the options in my helper.
def options_for_seasons
['Spring', 'Summer', 'Autumn', 'Winter']
end
I went with:prompt => 'Select One'because I only wanted the "Select One" option to be listed in the edit form if a season had not been previously selected.
Adding the ["Select One", ""] causes the edit screen to alway display "Select One" rather than the stored value.
Rails 3.1 (2012 Aug 17)
could be <%= f.select :phone_type, options_for_select(["Cell", "Work", "Office", "Home", "Other"]), :prompt => "Select One", :class => 'genForm_dropBox' %>