ruby-on-railsrubygemsform-with

country_select gem throws "NoMethodError", undefined method `country_select' for #


I am new to rails but I think I'm following every step for using this gem. However, I'm getting a NoMethodError. My steps: I ran gem install country_select -v 3.1.1 in terminal. I stopped my server, ran bundle install, restarted my server. I added the method to my code. I tried adding it to my gem file but that caused a different error preventing my Rails Server from running (see below for details). It seems like installing via the terminal should be enough, according to the github doc.

The only other stackoverflow issue that seemed to have the same problem as me says it was fixed by restarting the server, but I've tried this several times and it hasn't worked. If anyone can help me here, I'd appreciate it very much!

note: Since my original question has been answered and the problem evolved, I posted a new question with my terminal issue here: country_select gem causes "Could not find i18n_data-0.10.0 in any of the sources" error

_form.html.erb

 <%= form_with(model: [ @contact, @contact.addresses.build ] ) do |form| %>
  <p>
    <%= form.label :street %><br>
    <%= form.text_field :street %>
  </p>
  <p>
    <%= form.label :town %><br>
    <%= form.text_field :town %>
  </p>
  <p>
    <%= form.label :zip %><br>
    <%= form.text_field :zip %>
  </p>
  <p>
    <%= form.label :state %><br>
    <%= form.text_field :state %>
  </p>
  <p>
    <%= form.label :country %><br>
    <%= form.country_select :country %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>

show.html.erb

<p>
  <strong>Contact:</strong>
  <p>
  <%= @contact.salutation %>
  <%= @contact.first_name %>
  <%= @contact.middle_name %>
  <%= @contact.last_name %>
  </p>
  <p>
  <strong>SSN:</strong>
  <%= @contact.ssn %><br>
  <strong>Date of Birth:</strong>
  <%= @contact.dob %><br>
  <strong>Comment:</strong>
  <%= @contact.comment %>
  </p>
</p>

<h2>Addresses</h2>
<%= render @contact.addresses %>

<h2>Add an address:</h2>
<%= render 'addresses/form' %>

<%= link_to 'Edit', edit_contact_path(@contact) %> |
<%= link_to 'Back', contacts_path %>

Here is the full error from terminal:

Completed 500 Internal Server Error in 307ms (ActiveRecord: 67.6ms | Allocations: 85354)


  
ActionView::Template::Error (undefined method `country_select' for #<ActionView::Helpers::FormBuilder:0x00007fd0dcc18428>):
    17:   </p>
    18:   <p>
    19:     <%= form.label :country %><br>
    20:     <%= form.country_select :country %>
    21:   </p>
    22:   <p>
    23:     <%= form.submit %>
  
app/views/addresses/_form.html.erb:20
app/views/addresses/_form.html.erb:1
app/views/contacts/show.html.erb:23

When I add the gem to my gemfile and run bundle install, I cannot restart the server because I get this error:

Could not find i18n_data-0.10.0 in any of the sources
Run `bundle install` to install missing gems.

bundle install returns the following:

The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Using rake 13.0.1
Using concurrent-ruby 1.1.7
Using i18n 1.8.5
Using minitest 5.14.2
Using thread_safe 0.3.6
Using tzinfo 1.2.7
Using zeitwerk 2.4.0
Using activesupport 6.0.3.3
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.10
Using rails-dom-testing 2.0.3
Using crass 1.0.6
Using loofah 2.7.0
Using rails-html-sanitizer 1.3.0
Using actionview 6.0.3.3
Using rack 2.2.3
Using rack-test 1.1.0
Using actionpack 6.0.3.3
Using nio4r 2.5.3
Using websocket-extensions 0.1.5
Using websocket-driver 0.7.3
Using actioncable 6.0.3.3
Using globalid 0.4.2
Using activejob 6.0.3.3
Using activemodel 6.0.3.3
Using activerecord 6.0.3.3
Using mimemagic 0.3.5
Using marcel 0.3.3
Using activestorage 6.0.3.3
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailbox 6.0.3.3
Using actionmailer 6.0.3.3
Using actiontext 6.0.3.3
Using public_suffix 4.0.6
Using addressable 2.7.0
Using bindex 0.8.1
Using msgpack 1.3.3
Using bootsnap 1.4.8
Using bundler 2.1.4
Using byebug 11.1.3
Using regexp_parser 1.7.1
Using xpath 3.2.0
Using capybara 3.33.0
Using childprocess 3.0.0
Using i18n_data 0.10.0
Using sixarm_ruby_unaccent 1.2.0
Using unicode_utils 1.4.0
Using countries 3.0.1
Using sort_alphabetical 1.1.0
Using country_select 4.0.0
Using ffi 1.13.1
Using jbuilder 2.10.1
Using rb-fsevent 0.10.4
Using rb-inotify 0.10.1
Using listen 3.2.1
Using method_source 1.0.0
Using pg 1.2.3
Using puma 4.3.6
Using rack-proxy 0.6.5
Using thor 1.0.1
Using railties 6.0.3.3
Using sprockets 4.0.2
Using sprockets-rails 3.2.2
Using rails 6.0.3.3
Using rubyzip 2.3.0
Using sassc 2.4.0
Using tilt 2.0.10
Using sassc-rails 2.1.2
Using sass-rails 6.0.0
Using selenium-webdriver 3.142.7
Using spring 2.1.1
Using spring-watcher-listen 2.0.1
Using turbolinks-source 5.2.0
Using turbolinks 5.2.1
Using web-console 4.0.4
Using webdrivers 4.4.1
Using webpacker 4.3.0
Bundle complete! 18 Gemfile dependencies, 80 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Solution

  • gem install installs a gem on your computer. If you want to include a gem in your rails app, you need to add it to the gemfile:

    gem 'country_select', '~> 4.0'
    

    I agree, most docs are a bit ambigious in that sense, but they are general because they cannot forsee what the user intends to do with the gem.