I'm rendering this code from a partial:
<%= f.country_select :country_code, {object: f.object, prompt: "Country"}, required: true %>
And my country code isn't saving to the database. What am I missing?
I don't have enough rep points to just make a comment, but did you add country_code to the controller_params in controller?
And also, should it be nested and added to permitted _params?
Related
I am using Rails 4.2 with Ruby 2.1.5
I have a table name "API" and has "status" column.
How do I create form_for radio buttons base on status data in view template?
Here's a simple example
<% form_for(...) do |f| %>
<%= f.radio_button :status, 'alpha' %>
<%= f.radio_button :status, 'beta' %>
<% end %>
For more details you should check the radio_button helper documentation.
How I can to pass an array like generated by a controler with the following code:
tipos= Type.all
#listadotipos=[]
tipos.each do |h|
#listadotipos.push(h.name)
end
The last code generate an array called #listadotipos. This array is passed to a html.erb view and I want to show all array components inside a Select field in the view.
The select field works this:
<%= f.select :make, options_for_select(["option1", "option2"]) %>
How I can do this. Please help me.
First of all your controller code can be cleaned up like so:
#listadotipos = Type.all.map(&:name)
Your view code should work if you use the variable instead of your hard coded array:
<%= f.select :make, options_for_select(#listadotipos) %>
With the below code inside the controller :
#listadotipos = Type.all.map { |type| [ type.name, type.id ] }
You can write as below :
<%= f.select :make, #listadotipos %>
Read 3.2 Select Boxes for Dealing with Models guide.
Try this in view
<%= f.select :make, Type.all.collect(&:name) %>
For id
<%= f.select :make, Type.all.collect{|type| [type.name, type.id] } %>
This are the solution. And I dont need code into the controller:
<%= f.select :make, options_from_collection_for_select(Type.all, :id, :name) %>
Thanks all
I'm in the middle of trying to create a form where one of the questions is to choose a specific language. I'm trying to use the language list gem here: https://github.com/scsmith/language_list . However, the documentation doesn't really show me how I would combine the list with a select_tag.
<%= form_for users_path, :method => "get" do %>
<%= label_tag "Select Country" %> <br>
<%= country_select(:user, :country, [], :include_blank => true) %> <br>
<%= label_tag "Language spoken" %>
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "id", "name") %>
<%= label_tag "City" %> <br>
<%= text_field_tag(:city) %>
<% end %>
gives me the error
undefined method 'id' for afr (af) - Afrikaans:LanguageList::LanguageInfo
on the line
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "id", "name") %>
Can anyone help me?
Don't use "id". The docs say you should use iso_639_1 (or iso_639_3 if you want 3-letter codes)
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "iso_639_1", "name") %>
The LanguageList class seems to return a hash of LanguageInfo instances, having attributes like name, type and code -- `options_from_collection_for_select' expects its first parameter to be the collection (the hash in this case), the second a method that will return the value you want to identify the item, and the third is a display string.
So when someone selects a language, what are you going to store in the database? Probably one of the codes, right? So in this were true, you would make the second argument a method that an instance of the collection would respond to, which (reading the source code of the gem) is either iso_639_1 or iso_639_3. name should already work.
So if you replace id with one of those two iso_nnn_n values, then the form should display. To actually save the language code in the database, you'll need a column in your database for it, which you may already have as language.
Sometimes it's make a lot of sense to store gem's data dump in the database.
here is sample with postgresql and rails https://github.com/serghei-topor/import-language-list-into-db-rails-sample
here is csv file of gem's data dump https://github.com/serghei-topor/language-list-csv
And select_tag will look like:
<%= select_tag "Language", options_from_collection_for_select(Language.where(is_common:true).order(:name), "id", "name") %>
my form is not tied to a particular model and looks like this:
<%= form_tag(:controller => 'orders' , :action => 'process_credit_card') do %>
... bunch of fields ...
<% end %>
carmen-rails' country_select helper looks like this
<%= f.country_select :country_code, {priority: %w(US CA)}, prompt: 'Please select a country' %>
however I do not have a form object f, I use helpers like <%= text_field_tag 'billing_address[phone]' %> to create my form, is there a way I can still use carmen in this form?
UPDATE: I am using ActiveMerchant for payment processing, I can create a form with form_for instead of form_tag but I don't know how, any pointers will be appreciated.
this should work...
<%= country_select :country_code, {priority: %w(US CA)}, prompt: 'Select' %>
I'm a few years late, but I ran into this problem tonight and found a solution that worked for me by crawling through the carmen-rails source code. Note that the second hash (as the fourth argument) can contain HTML options like class.
<%= country_select(nil, :country_code, { priority: %w(US CA) }, {}) %>
With the other solutions (that did not have nil as the first argument) I was having serialization issues with my AJAX search functionality. The entire hash (including priority and other code) would be serialized with the request. When calling it like I have above, this is no longer an issue and only the country code is serialized. Hopefully this will help someone in the future who ran into the same issue we did.
Please use
<%= country_select(nil,:country, { priority: %w(US CA) , prompt: 'Select Country'},:class=>"form-control") %>
Use above with form_tag
<%= f.country_select :country, {priority: %w(US CA), prompt: 'Select Country'},:class=>"form-control" %>
and use above with form_for
The correct way to do this isn't by using the country_select gem but a dependency it has countries gem and plain Rails form helpers select_tag and options_for_select: Using country_select gem with form_tag and no model
Try:
<%= country_select_tag :country_code, {priority: %w(US CA)}, prompt: 'Please select a country' %>
According to the ActionView documentation. Quote:
The text of label will default to the attribute name unless a translation is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly.
I have a "user" model and a registration form. Here's a snippet of the relevant part:
<% form_for(#user) do |f| %>
...
<p>
<%= f.label :username %>
<%= f.text_field :username, :class => 'full_width' %>
</p>
...
<% end %>
Dots hide unimportant code.
As I understand the documentation, if I provide a translation in my locale file, in this case :dk, my dk.yml looking like so:
dk:
views:
labels:
user:
username:
"blahblah"
Rails should translate the label text and insert "blahblah" instead of "Username".
This is not happening, so I must have missed something. Any help appreciated.
In Rails 3.1 that is a little bit changed.
<% form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
en:
helpers:
label:
post:
title: 'Customized title'
I think I found another solution here.
My app was version 2.3.5. I've now changed it to 2.3.8 and <%= f.label :username %> now uses the translation in:
dk:
activerecord:
attributes:
user:
username:
I found the hint in this ticket:
https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n
That's because the label method you are calling is not the one from ActionView::Helpers::FormHelper but is in fact the label_tag method from ActionView::Helpers::FormTagHelper. The form_for method is rewriting the code in the given block by adding _tag to the used form helpers. So you're not looking at the documentation for the right method!
I've not yet used that method, as sometimes the label for a field can be different from multiple forms using the same model, so I've written my own helper.