How to use the Countries gem - ruby-on-rails

I am trying to use the Countries Gem, but had some basic questions on how to incorporate this gem after I've bundle-installed it.
Do I need to create a new controller/model to access the countries?
How do I create a simple select drop-down to show a list of countries for a user to select?
Where are all the countries stored? (I saw the data file in the Gem, but need some clarity how to bring that into my own app)

1) You don't need a new controller/model to access the countries
2) There is an example app on the README page which shows you how to use forms and dropdowns.
3) The countries are stores within the app. I believe country_select includes the ISO 3166 gem to get the list of countries. You can view the data in the countries.yaml file
If want to know anything else, I recommend looking at the example app. It provides a good example of how to use the gem.

You do not need to create a new controller/model to work with the gem.
In order for you to create dropdown, just install the country_select gem (as stated in the doc)
Then to use it, just do that in your views:
country_select(:your_model_name, :your_attribute_name)
To integrate it in a form_for with some extra params like Bootstrap classes or default country selected:
<%= form_for #message, url: contact_path, html: { :class => "form-horizontal " } do |f| %>
<div class="form-group">
<%= f.label :country, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.country_select :country, ["United States"], {}, { :class => "form-control" } %>
</div>
</div>
<%= f.submit "Submit", :class => "btn btn-default" %>
<% end %>
For the exact options you have w/ this method, see here:
country_select(method, priority_or_options = {}, options = {}, html_options = {})
Hope it helps !

Related

How to use Bootstrap Multiselect in Rails views?

I am trying to implement bootstrap multiselect field in my rails app.
Making use of the bootstrap-multiselect_rails gem found here (https://github.com/TrevorS/bootstrap-multiselect_rails)
Have it installed and configured successfully but in my form am not able to select multiple vales. It allows me to select only an single value.
Right now my code looks like this:
<%= f.collection_select :role_pm, User.where(:user_role => 'Project Manager'), :name, :name, {}, {:multiple => 'true'}, {class: "role_pm"} %>
Where am I going wrong?
Finally got this working. I have Update the line of code in this answer which has caused me a lot of agony over the past 2 days or so
<%= f.collection_select :role_pm, User.where(:user_role => 'Project Manager'), :name, :name, {}, :multiple => 'true', :class => 'role_pm' %>
Looks like I have passed both multiple and class attributes as seperate arrays which was really not needed in the first place.
.You need to initialize Multiselect using the js.
here comes my working code:-
###HTML FILE
##my controller has #event_types to autopopulate the values as well for edit action
<label for="events" class="control-label form-group col-md-12">Event Type: </label>
<div class="form_group col-md-12">
<div class="btn-group">
<%= select_tag("event_types", options_for_select(#event_types.pluck(:name),:multiple=>true,:required=>true) %>
</div>
</div>
###js FILE-initialise using id/class for multiselect
$('#event_types').multiselect({
enableFiltering: true,
filterBehavior: 'text',
enableCaseInsensitiveFiltering: true,
nonSelectedText: 'Select the type of events'
});
you can use selected attribute in select tag to select those values during edit action.Just pass it from controller..example
:selected => #event_types.new_record? ? nil : #event_types.pluck(:name)
rewriting your query...
<%= select_tag("event_types", options_for_select(#event_types.pluck(:name),:multiple=>true,:required=>true) %>
changes to :Showing names in select dropdown list
<%= select_tag("role_pm", options_for_select(User.where(:user_role => 'Project Manager').pluck(:name).uniq,:multiple=>true,:required=>true) %>

How can I set a selected box values for form

Sorry for this rather simple question, but I know almost nothing of rails.
What I want to do is that when I'm registering an "Articulo" I would like to choose some default values that they would be given by an attribute of a model, specifically, the attribute "rut_prov" from the model "Proovedor".
This is what I have.
articulos_controller.rb
def new
#articulo = Articulo.new
#proveedors = Proveedor.all
respond_with(#articulo)
end
articulos/new.html.erb
<div class="container">
<h1>Nuevo artículo de venta</h1>
<%= render 'form' %>
articulos/_form.html.erb
<div class="field">
<%= f.label :rut_del_proveedor %><br>
<%= f.select :rut_prov, #proveedors, :class => 'genForm_dropBox' %>
</div>
view
So, I know the problem is <%= f.select :rut_prov, #proveedors, :class => 'genForm_dropBox' %>, but I don't know how to make appear as options the values of "rut_prov" of the records I have registered of the model "Proovedor".
You can use collection_select method. The following example displays the result of rut_prov method call on view and passes rut_prov of selected as param:
<%= f.collection_select :rut_prov, #proveedors, :rut_prov, :rut_prov, class: 'getForm_dropBox' %>
BTW, I strongly recommend using English variables/classes/methods names. So it should be Provider instead of Provedoor, Article instead of Articulo etc.

Best way to dropdown a list of country in rails 4

i have a form where users input some information and I want to add to this form a country and I am wandering what is the best way to do this (seed data, Csv, ...) i don't want to use a gem
Since this is essentially static data, I wouldn't even store it into the database. I would create a PORO that had a class method that returns a 2 dimensional array (country with country abbreviation). It's just a matter of sourcing that data.
Here's a list you can use.
https://github.com/karmi/localized_country_select/blob/master/locale/en.rb
I use the country_select gem. It is very simple to use. Just supply model and attribute as parameters:
country_select("user", "country")
Here is an example from one of my forms:
<%= f.fields_for (:address) do |b2| %>
<%= b2.input :street %>
<%= b2.input :city, :label => "City/Suburb" %>
<%= b2.input :state %>
<%= b2.input :post_code %>
<div class="control-group">
<label class="control-label">Country</label>
<div class="controls">
<%= b2.country_select :country, ["Australia", "New Zealand"] %>
</div>
</div>
<% end %>

Different types of form helpers in rails

I'm newish to rails, and scaffolding generates form elements like this:
<%= f.label :completed_at %>
and the form helper documentation for rails talks about them in a different form, as functions:
<%= label( :giveaway, :completed_at, "Closing Date", :class => "control-label" ) %>
Where can I find the documentation for the former? is there a name for this version of a form helper?
http://guides.rubyonrails.org/form_helpers.html may help
I would just call it a form label helper.
<%= f.label :completed_at %> refers to item f during the iteration through a form builder object
<%= label( :giveaway, :completed_at, "Closing Date", :class => "control-label" ) %> is a static method aka "form helper" method used to generate any label on the fly, not necessarily in the context of a form builder object

Howto make twitter bootstrap compatible checkbox labels in simple_form?

I'm trying to get two labels for one check box as described in Twitter Bootstrap's documentation. (see http://twitter.github.com/bootstrap/base-css.html#forms --> horizontal forms --> checkbox (below text input))
So what I want to display is a label for the description on the left, the check box itself and a hint right next to it on the right.
The standard implementation of twitter bootstrap in simple_form gem creates a <p> tag for displaying the hint since it tries to be consistent for all kinds of inputs.
I now want to create a custom wrapper for "bootstrap checkboxes" in the simple_form initializer but I just cannot figure out how to solve this.
This is how I currently implemented it using bare rails (erb):
<div class="control-group">
<%= f.label :recurring, :class => 'control-label' %>
<div class="controls">
<%= f.label :recurring, :class => 'checkbox' do %>
<%= f.check_box :recurring %>
<%= t('.recurring_hint') %>
<% end %>
</div>
</div>
Can you help me or at least try to explain how these custom wrapper thing works? Thank you!
Edit: Let me ask my question more precisely: Can I use simple_form's wrappers API to use a label as a wrapper?
Solution:
Update to newest version of simple_form (2.0.2, had 2.0)
Override BooleanInput:
app/inputs/boolean_input.rb
class BooleanInput < SimpleForm::Inputs::BooleanInput
def input
if nested_boolean_style?
template.label_tag(nil, :class => "checkbox") {
build_check_box + inline_label
}
else
build_check_box
end
end
end
_form.html.erb
Call in template:
<%= f.input :recurring, :inline_label => t('.recurring_hint') %>
With latest simple_form, the option is available out of the box.
You can install simple_form with bootstrap suport, so you don't need to write the wrappers like control-groups, etc.
Check this link on installation:
https://github.com/plataformatec/simple_form#twitter-bootstrap
What you need to do is just run the command:
rails generate simple_form:install --bootstrap
In this page you can see some examples, including horizontal checkboxes with bootstrap and simple_form: http://simple-form-bootstrap.plataformatec.com.br/articles/new
If you really want to use it the way you are doing, you need to add the class "inline" along with the "checkbox" on the label.

Resources