To use the :country input, please install a country_select plugin

前端 未结 6 1301
-上瘾入骨i
-上瘾入骨i 2021-01-18 03:19

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 guide

相关标签:
6条回答
  • 2021-01-18 03:46

    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.

    0 讨论(0)
  • 2021-01-18 03:48

    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
    
    0 讨论(0)
  • 2021-01-18 04:00

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

    0 讨论(0)
  • 2021-01-18 04:03

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

    0 讨论(0)
  • 2021-01-18 04:09

    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.

    0 讨论(0)
  • 2021-01-18 04:10

    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
    
    0 讨论(0)
提交回复
热议问题