Home>

HereWhen I implemented the tag function with reference to, an error occurred.

If i enter only title and content in the browser, the post will succeed. It feels like an error occurs if you enter even one character in the taglist. Also, the post will succeed even if you do not enter anything.

NoMethodError (undefined method `[] ='for nil: NilClass):
app/controllers/posts_controller.rb: 30: in `create'
undefined method `[] ='for nil: NilClass
Source code

posts_controller.rb

class PostsController</pre>
<p><br />
post.rb</p>
<pre><code data-language = "Ruby">class Post</pre>
<p><br />
_form.html.erb</p>
<pre><code data-language = "Ruby"><% = form_with (model: post) do | form |%><% if post.errors.any?%>       <% = pluralize (post.errors.count, "error")%>prohibited this post from being saved:     <ul>    <% post.errors.each do | error |%>      <li><% = error.full_message%></li>    <% end%>  </ul>  <% end%>  <% = form.label: title%><% = form.text_field: title%>    <% = form.label: content%><% = form.text_area: content%>    <% = form.label: tag_list%><% = form.text_area: tag_list, value: @ post.tag_list.join (',')%><% tag.each do | tag |%><% = form.check_box: tag_list, {multiple: true},

 "# {tag.name}", nil%><% = form.label "# {tag.name} (# {tag.taggings_count})"%><% end%>  <% = form.submit%><% end%>


gemfile

source'https://rubygems.org'
git_source (: github) {| repo | "https://github.com/#{repo}.git"}
ruby '2.7.2'
gem'acts-as-taggable-on'
# Bundle edge Rails instead: gem'rails', github:'rails/rails'gem'rails','~>6.1.0'
# Use sqlite3 as the database for Active Record
gem'sqlite3','~>1.4'
#Use Puma as the app server
gem'puma','~>5.0'
#Use SCSS for stylesheets
gem'sass-rails','>= 6'
#Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem'webpacker','~>5.0'
#Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem'turbolinks','~>5'
#Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem'jbuilder','~>2.7'
#Use Redis adapter to run Action Cable in production
# gem'redis','~>4.0'
# Use Active Model has_secure_password
# gem'bcrypt','~>3.1.7'
# Use Active Storage variant
# gem'image_processing','~>1.2'
#Reduces boot times through caching;required in config/boot.rb
gem'bootsnap','>= 1.4.4', require: false
group: development,: test do
  #Call'byebug' anywhere in the code to stop execution and get a debugger console
  gem'byebug', platforms: [: mri,: mingw,: x64_mingw]
end
group: development do
  #Access an interactive console on exception pages or by calling'console' anywhere in the code.
  gem'web-console','>= 4.1.0'
  # Display performance information such as SQL time and flame graphs for each request in your browser.
  #Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
  gem'rack-mini-profiler','~>2.0'
end
group: test do
  #Adds support for Capybara system testing and selenium driver
  gem'capybara','>= 3.26'
  gem'selenium-webdriver'
  #Easy installation and use of web drivers to run system tests with browsers
  gem'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem'tzinfo-data', platforms: [: mingw,: mswin,: x64_mingw,: jruby]
What I tried

I thought that it was because the version was not specified in the gemfile, and when I specified it, I returned it because such an error appeared.

gem'acts-as-taggable-on','~>6.0'
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies ...
Bundler could not find compatible versions for gem "activerecord":
  In snapshot (Gemfile.lock):
    activerecord (= 6.1.0)
  In Gemfile:
acts-as-taggable-on (~>6.0) x64-mingw32 was resolved to 6.0.0, which
depends on
      activerecord (~>5.0) x64-mingw32
    rails (~>6.1.0) x64-mingw32 was resolved to 6.1.0, which depends on
      activerecord (= 6.1.0) x64-mingw32
Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
Supplementary information (FW/tool version, etc.)

Windows 10
ruby 2.7.2p137
Rails 6.1.0

  • Answer # 1

    app/controllers/posts_controller.rb: 30: in `create'

    Where is this 30th line?

    Probably because there is no conditional branch when @post is empty.

Related articles