如何解决country_select gem抛出“ NoMethodError”,#的未定义方法`country_select'
我是Rails的新手,但我认为我正在遵循使用此gem的所有步骤。
但是,我收到了NoMethodError。
我的步骤:
我在终端机上运行了gem install country_select -v 3.1.1
。
我停止了服务器,运行捆绑安装,然后重新启动了服务器。
我将方法添加到了代码中。我尝试将其添加到我的gem文件中,但这导致了另一个错误,导致我的Rails Server无法运行(有关详细信息,请参见下文)。根据{{3}}
似乎与我说的问题相同的唯一另一个stackoverflow问题是通过重新启动服务器来解决的,但是我已经尝试了几次,但没有成功。如果有人可以在这里帮助我,我将非常感谢!
注意:由于我的原始问题已得到回答并且问题有所发展,因此我在此处发布了一个新问题,其终期问题为:github doc.
_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 %>
这是终端机的完整错误:
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
当我将gem添加到我的gemfile中并运行bundle install时,由于出现此错误,我无法重新启动服务器:
Could not find i18n_data-0.10.0 in any of the sources
Run `bundle install` to install missing gems.
捆绑安装返回以下内容:
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.
解决方法
gem install
在您的计算机上安装一个gem。如果要在rails应用程序中包含gem,则需要将其添加到gemfile中:
gem 'country_select','~> 4.0'
我同意,大多数文档在这个意义上有点模棱两可,但它们之所以笼统,是因为它们无法预知用户打算如何使用gem。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。