To use the :country input, please install a countr

2020-04-05 06:46发布

问题:

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

回答1:

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


回答2:

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


回答3:

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.



回答4:

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.



回答5:

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



回答6:

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 :)