前提・実現したいこと
こちらを参考にタグ機能を実装していったところ、エラーが発生しました。
発生している問題・エラーメッセージ
ブラウザ上でtitleとcontentのみを入力した場合はpostは成功します。taglistに一文字でも入力するとエラーが出るといった感じです。また、何も入力しなくてもpostは成功します。
NoMethodError (undefined method `[]=' for nil:NilClass): app/controllers/posts_controller.rb:30:in `create' undefined method `[]=' for nil:NilClass
ソースコード
posts_controller.rb
ruby
1class PostsController < ApplicationController 2 before_action :set_post, only: [:show, :edit, :update, :destroy] 3 4 # GET /posts 5 # GET /posts.json 6 def index 7 @posts = Post.all 8 @tags = Post.tag_counts_on(:tags).most_used(20) 9 end 10 11 # GET /posts/1 12 # GET /posts/1.json 13 def show 14 @post = Post.find(params[:id]) 15 @tags = @post.tag_counts_on(:tags) 16 end 17 18 # GET /posts/new 19 def new 20 @post = Post.new 21 end 22 23 # GET /posts/1/edit 24 def edit 25 end 26 27 # POST /posts 28 # POST /posts.json 29 def create 30 @post = Post.new(post_params) 31 32 respond_to do |format| 33 if @post.save 34 format.html { redirect_to @post, notice: 'Post was successfully created.' } 35 format.json { render :show, status: :created, location: @post } 36 else 37 format.html { render :new } 38 format.json { render json: @post.errors, status: :unprocessable_entity } 39 end 40 end 41 end 42 43 # PATCH/PUT /posts/1 44 # PATCH/PUT /posts/1.json 45 def update 46 respond_to do |format| 47 if @post.update(post_params) 48 format.html { redirect_to @post, notice: 'Post was successfully updated.' } 49 format.json { render :show, status: :ok, location: @post } 50 else 51 format.html { render :edit } 52 format.json { render json: @post.errors, status: :unprocessable_entity } 53 end 54 end 55 end 56 57 # DELETE /posts/1 58 # DELETE /posts/1.json 59 def destroy 60 @post.destroy 61 respond_to do |format| 62 format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 63 format.json { head :no_content } 64 end 65 end 66 67 private 68 # Use callbacks to share common setup or constraints between actions. 69 def set_post 70 @post = Post.find(params[:id]) 71 end 72 73 # Only allow a list of trusted parameters through. 74 def post_params 75 params.require(:post).permit(:title, :content, :tag_list) 76 end 77end
post.rb
ruby
1class Post < ApplicationRecord 2 acts_as_taggable 3 acts_as_taggable_on :skills, :interests 4end
_form.html.erb
ruby
1<%= form_with(model: post) do |form| %> 2 <% if post.errors.any? %> 3 <div id="error_explanation"> 4 <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> 5 6 <ul> 7 <% post.errors.each do |error| %> 8 <li><%= error.full_message %></li> 9 <% end %> 10 </ul> 11 </div> 12 <% end %> 13 14 <div class="field"> 15 <%= form.label :title %> 16 <%= form.text_field :title %> 17 </div> 18 19 <div class="field"> 20 <%= form.label :content %> 21 <%= form.text_area :content %> 22 </div> 23 24 <div class="field"> 25 <%= form.label :tag_list %> 26 <%= form.text_area :tag_list, value: @post.tag_list.join(',') %> 27 </div> 28 29 <% tag.each do |tag| %> 30 <%= form.check_box :tag_list, { multiple: true }, "#{tag.name}", nil %> 31 <%= form.label " #{tag.name}(#{tag.taggings_count})"%> 32 <% end %> 33 34 <div class="actions"> 35 <%= form.submit %> 36 </div> 37<% end %>
gemfile
ruby
1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.7.2' 5gem 'acts-as-taggable-on' 6 7# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 8gem 'rails', '~> 6.1.0' 9# Use sqlite3 as the database for Active Record 10gem 'sqlite3', '~> 1.4' 11# Use Puma as the app server 12gem 'puma', '~> 5.0' 13# Use SCSS for stylesheets 14gem 'sass-rails', '>= 6' 15# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 16gem 'webpacker', '~> 5.0' 17# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 18gem 'turbolinks', '~> 5' 19# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 20gem 'jbuilder', '~> 2.7' 21# Use Redis adapter to run Action Cable in production 22# gem 'redis', '~> 4.0' 23# Use Active Model has_secure_password 24# gem 'bcrypt', '~> 3.1.7' 25 26# Use Active Storage variant 27# gem 'image_processing', '~> 1.2' 28 29# Reduces boot times through caching; required in config/boot.rb 30gem 'bootsnap', '>= 1.4.4', require: false 31 32group :development, :test do 33 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 34 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 35end 36 37group :development do 38 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 39 gem 'web-console', '>= 4.1.0' 40 # Display performance information such as SQL time and flame graphs for each request in your browser. 41 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 42 gem 'rack-mini-profiler', '~> 2.0' 43end 44 45group :test do 46 # Adds support for Capybara system testing and selenium driver 47 gem 'capybara', '>= 3.26' 48 gem 'selenium-webdriver' 49 # Easy installation and use of web drivers to run system tests with browsers 50 gem 'webdrivers' 51end 52 53# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 54gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
試したこと
gemfileでバージョンを指定していないのが原因かと思い指定してみたところこのようなエラーが出てきたため戻しました。
gemfile
1gem 'acts-as-taggable-on', '~> 6.0'
console
1Fetching gem metadata from https://rubygems.org/............. 2Fetching gem metadata from https://rubygems.org/. 3Resolving dependencies... 4Bundler could not find compatible versions for gem "activerecord": 5 In snapshot (Gemfile.lock): 6 activerecord (= 6.1.0) 7 8 In Gemfile: 9acts-as-taggable-on (~> 6.0) x64-mingw32 was resolved to 6.0.0, which 10depends on 11 activerecord (~> 5.0) x64-mingw32 12 13 rails (~> 6.1.0) x64-mingw32 was resolved to 6.1.0, which depends on 14 activerecord (= 6.1.0) x64-mingw32 15 16Running `bundle update` will rebuild your snapshot from scratch, using only 17the gems in your Gemfile, which may resolve the conflict.
補足情報(FW/ツールのバージョンなど)
Windows10
ruby 2.7.2p137
Rails 6.1.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/31 11:54 編集
2020/12/31 12:45
2020/12/31 12:52
2020/12/31 13:02
2020/12/31 13:11
2020/12/31 13:16
2020/12/31 13:28
2020/12/31 13:33
2020/12/31 13:54