To use the :country input, please install a country_select plugin - ruby-on-rails

I have a country attribute to my guidelines model. I don't want to use a plugin, I just want to have country as a string. Everything is working until I try to edit a guideline in activeadmin and then I get the error message:
ActionView::Template::Error (To use the :country input, please install
a country_select plugin, like this one:
https://github.com/jamesds/country-select):
1: insert_tag renderer_for(:edit)
in my form I have
<%= f.input :country, as: :string%>
in my admin/guidelines.rb I have
index do
column :title
column :specialty
column :content
column :hospital
column :country
column :user
default_actions
end

I'm not sure where you get this form code from but I had the same issue with Active Admin and resolved it by explicitly instructing the form to treat the field as a string:
ActiveAdmin.register Guideline do
form do |f|
f.inputs 'Details' do
f.input :country, :as => :string
end
f.actions
end
end

First you need to add the gem in GemFile
gem 'country-select'
Create a helper file '/app/helpers/active_admin/views_helper.rb'. Add the below code
module ActiveAdmin::ViewsHelper
def country_dropdown
ActionView::Helpers::FormOptionsHelper::COUNTRIES
end
end
In your view file use
form do |f|
f.inputs do
f.input :country, as: :select, collection: country_dropdown
end
f.actions
end

Use country_select. Seems to work fine with Rails 4.1 if you're doing this recently. Plus Rails old repo links to this one rather than country-select.

You're using ActiveAdmin, so you're also using Formtastic.
In Formtastic, in the file formtastic/lib/formtastic/inputs/country_input.rb it clearly says:
# Rails doesn't come with a `country_select` helper by default any more, so you'll need to do
# one of the following:
#
# * install the [country_select](https://github.com/stefanpenner/country_select) gem
# * install any other country_select plugin that behaves in a similar way
# * roll your own `country_select` helper with the same args and options as the Rails one
I would add gem 'country-select' to your Gemfile and do a bundle install as is the simplest and fastest solution.

I guess you could install the gem, and then override the display in active_admin.

I recommend you to use the country-select:
gem 'country-select'
I spent a lot of hours to find out why country-select is not working in my form :)

Related

Rails: Undefined local variable or method with Country-State-Select gem

I am currently working on a Rails application where I need to list countries and their corresponding states/provinces. For this function, I am using the Country-State-Select gem.
I have included the gem in my Gemfile
gem 'country_state_select'
And installed it
bundle install
However, the official documentation used simple_form objects while I am using the default rails form objects.
Here's my form field for country, which works very fine:
<%= form.label :country %>
<%= form.select :country_field, collection: CountryStateSelect.countries_collection %>
However, the form field for state which is below:
<%= form.label :state %>
<%= form.select :state_field, CountryStateSelect.state_options(form: form, field_names: { country: country_field, state: state_field } ) %>
keeps throwing errors:
undefined local variable or method `country_field'
Here's the simple form implementation of it in the Country-State-Select gem documentation, which I am trying to convert to the default rails form objects:
<%= options = { form: f, field_names: { :country => :country_field, :state => :state_field } }
f.input :state_field, CountryStateSelect.state_options(options) %>
I would appreciate some form of help. Thank you.
I just learnt from Arvind Vyas the owner of the Country-Select-Select gem in this Stackoverflow answer that the gem depends on the simple_form gem.
So the best bet is to integrate the simple_form gem into the application or look for other alternatives, since the default rails form which I am using will not work for the implementation.

Rails active admin free search on select element

I am using active admin gem.
i have questions table that have relationship with user_id, when i want to add new questions the select element is showing with tons of users, there is a way to make this select element along with search input for user name?
ActiveAdmin.register Question do
permit_params :difficulty, :title, :user
form do |f|
actions
inputs 'Question Details' do
input :user, :as => :select, :collection => User.all
input :difficulty, as: :select, collection: [1,2,3,4]
input :title
end
actions
end
end
You can use a javascript plugin, I'd thoroughly recommend chosen
It will dynamically turn a regular form select into a searchable select with a simple Javascript call.
You can checkout some demos here:
http://harvesthq.github.io/chosen/
You can integrate it into activeadmin by adding the chosen-rails gem to your Gemfile
gem 'chosen-rails'
Then add the javascript to your app/assets/javascripts/active_admin.js.coffee
#= require chosen-jquery
Add the css to your app/assets/stylesheets/active_admin.scss
#import "chosen"
You can add it to all your select inputs by simply adding the following to app/assets/javascripts/active_admin.js.coffee
$ ->
$("#active_admin_content select").chosen()
You could also limit it to certain select inputs by adding a class to active admin inputs and only targeting those selects with a specific class like chosen-select

Create dropdown menu signup page with devise

I would like to create a dropdown menu for a list of countries in my signup page with devise. I understand that I need to create a migration
rails g migration add_countries_to_user country:string
and then I have to use create the form in my view page
<%= f.select :countries, options_for_select(%w[Alfganistan, Albania, Algeria...]) %>
I would like to know if my form correct and where can I put the countries list in because it is not right to write 200+ countries in the view page right?
Thanks.
As suggested, you can use country_select. Or, you can do it on your own as:
Create an initializer which contains list of countries (or anything in particular you want) config/initializers/countries.yml
countries:
- Afghanistan
- United States
- ...
Load it in database by creating a rake task as:
lib/tasks/load_countries.rb
namespace :db do
desc "Loads countries in database"
task :load_countries => :environment do |t|
countries_list = YAML.load("#{Rails.root}/config/initializers/countries.yml")['countries']
countries.each do |country|
Country.find_or_create_by_name(country)
end
end
end
Whenever you add any countries in yml, you can populate it by invoking this rake task: rake db:load_countries.
Maintain a model Country :
class Country < ActiveRecord::Base
validates :name, presence: true, uniqueness: { case_insensitive: true }
end
I am considering that a user belongs_to 1 country above, and a country has_many users. In your view, :
f.select :country, options_from_collection_for_select(Country.all, :id, :name)
Note: I am using association approach above, since it will make it easier to make queries against this field in future, unlike saving an actual string in user.
Use the country_select gem.
# Gemfile
gem 'country_select'
form:
country_select("user", "country")
Apart from gem and Countries read from YML.
One more option is creating a method in your helper
File : app/helpers/country_helper.rb
def get_countries
{:1=>Africa,:2=>"America"}
end
In Views you can use this way
<%= options_from_collection_for_select(get_countries, :id, :name) %>
Look up rails cast #88 revised dynamic select menus. What you need is method call grouped_collection_select in which you will map out the item you need based on how they corresponded to one another
You could do this as a helper method. eg, in your users_helper.rb you could list the selections:
def country_options
[
['Afghanistan'],
['Albania'],
...
['Zimbabwe']
]
end
Then, your selector pulls from that helper method:
<%= f.select :country, options_for_select(country_options), { prompt: 'Choose Country' } %>

Rails 4 acts_as_taggable_on :tag_list => [] strong param not working?

I am trying to use tagging on my notes model through this gem. However, even though I have explicitly added (2 seperate ways) :tag_list => [] to my strong params of my notes controller when ever I try and submit them, I still get an unpermitted parameter error in the logs? I have ran bundle install, and migrate as well.
Here are my files:
#/models/note.rb
class Note < ActiveRecord::Base
belongs_to :user
acts_as_taggable
validates_presence_of :name, :note_text, :note_style, :note_description
end
#/controllers/notes_controller.rb
.
.
.
def note_params
params.require(:note).permit(:name, :note_style, :note_text, :note_description, :tag_list => [])
end
and my notes form:
.form-group
= f.label :tag_list, "Tags (seperate by comma)"
= f.text_field :tag_list, class: 'form-control'
I followed everything from the gem but I still can't get it to work.
I actually got it to work by making my strong params by adding just :tag_list. Any idea why this works and not how they specified to do it in the gem documentation?
#/controllers/notes_controller.rb
.
.
.
def note_params
params.require(:note).permit(:name, :note_style, :note_text, :note_description, :tag_list, :tag_list => [])
end
The acts-as-taggable-on gem uses polymorphic association. In your case param as an empty array could not be initialized which might have caused the problem. Hope this clears your confusion. :-)
:tag_list => [] is needed when you're using a select tag in your form because a select tag returns an array when the form is submitted.
Since you are using a text field instead of a select tag, you are not returning an array when the form is submitted but a single value (the string in the text field), so you only need :tag_list in your permit parameters list.
Try using this in the list of permitted params
:tag_list => [:name, :taggings_count, :count]

simple_form input with multiple fields

I'm not quite sure what the correct terms are, but what I'm trying to do is in a form (preferably using the simple_form gem) have one of the inputs, :maximum, use both a text field and select box. The user would type in the text box a number, and then select from a dropdown box of hours, days, or months. So 21 days, 3 months, 3 hours, etc. When the form was submitted I would convert that to days and store it in the database. I know how to change the input type in simple_form, but is it possible to have two inputs for one variable?
Sure :) Here is my idea:
First, you define accessors in your user model:
attr_accessor :thing, :another_thing, :and_another_thing
Then in your view, 'inside' form_for helper, you could write for example:
<%= form.input :thing, :as => :boolean %>
<%= form.input :another_thing, :as => :text %>
...or whatever you want. (Note: I am using formtastic here. You should consider using Rails methods if you're not using formtastic gem. )
Finally, you define a callback in you user model:
before_create :build_my_fancy_record
def build_my_fancy_record
self.storage_field = "#{thing} #{another_thing}"
end

Resources