How to write prompt to set default select option - ruby-on-rails-3

I want to display default select option as "select Size" here. How to give it this select option?
Any help is appreciated.
This is my slim file:
= select_tag "standard_size_id",
options_from_collection_for_select(#standard_sizes, "id", "name"), include_blank: true, class: 'form-control'

You can try this way just add prompt attribute and remove include_blank
<%= select_tag "standard_size_id", options_from_collection_for_select(#standard_sizes, "id", "name"), class: 'form-control', prompt: 'Select Size' %>

Related

Simple form input as dropdown not select box

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.

How to create formtastic form with multiple language support (Internationalization)

I need to set up two languages for a single form using internationalization.
This is the proposed form page
<%= semantic_form_for #detail do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :dob %>
<%= f.input :gender, :as => :radio, :label => "Gender", :collection => [["Male", 'male'], ["Female", 'female']] %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
This is wk.yml file
wk:
formtastic:
labels:
detail:
dob: "Data of birtha"
name: "Youre Nama"
gender: "Gendera""
This is en.yml file
en:
formtastic:
labels:
detail:
dob: "Date of Birth"
name: "Your Name"
gender: "gender"
I have added Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true in formtastic.rb initializer.
I was successful in using en.yml.
I need to switch from 'en' to 'wk' and vise-verse.
How to achieve it via drop down box?
That's not something related to formstatic, but rather to your rails code.
All you have to do in order to switch to wk is
http://guides.rubyonrails.org/i18n.html
I18n.locale = :wk
In order to let your client to choose his language for the website, probably a good place to start is on this link: http://guides.rubyonrails.org/i18n.html
If all you want is to update the form (and not the rest of the website) in different languages on a user action, like selecting the language from a select box, you can use an ajax listener on the select box, that could require something like "http://www.yourwebsite.com/:locale/form/new" which will answer with an ajax action and will replace your form with the selected language (so on :locale you will pass the value of your select box for the language).

how to send select_tag value in form parameter in rails 3

<%= form_tag url_for(new_manage_listing_media_path(3)),:method => 'get' do -%>
<%= label_tag ':name', "Choose media:" %>
<%= select_tag "id", options_from_collection_for_select(#listing, "id", "name")%>
<%= submit_tag("OK") %>
<% end %>
actually what i want is in form_tag url_for(new_manage_listing_media_path(3)), instead of 3 i want to put the select_tag's selected value.
Thanks,
ariv
You have to use JavaScript to do what you're saying. Add an onUpdate handler to the select field, and update the action attribute for the form.
However, I think you can just leave out the three, and then the id value from the select will be used instead. Otherwise, name the select_tag something else, like lookup_id and use Model.find(params[:id] || params[:lookup_id]) in the controller, where Model is the model you're looking up.

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

how to use urlhelper to include rails 3 custom data- attribute

I am using Rails 3 and found that if I add
:remote => :true, there will be added to the tag the data-remote = true attribute. But I can't find a way to add custom data- attributes to the urlhelper. The followings won't work:
<%= link_to projects_path, :history => "new"%>
<%= link_to projects_path, :data-history => "new"%> #this throws an error
<%= link_to projects_path, :data_history => "new"%>
What I want to generate is:
New Project
anyone?
What about:
<%= link_to 'New Project', new_project_path, 'data-history' => 'new' %>
( http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to )
This is an elegant solution:
<%= link_to "foo", foo_path, data: { foo: "bar" } %>