前提・実現したいこと
Railsで非言語処理の設定をしたいです。
仮想環境では問題なく動いておりますが、本番環境ではエラーになってしまい原因がわかりませんでした。
ご教授いただけますと幸いです。
発生している問題・エラーメッセージ
Completed 500 Internal Server Error in 281ms (ActiveRecord: 1.2ms NoMethodError (undefined method `[]' for nil:NilClass): lib/language.rb:27:in `get_data'
該当のソースコード
controller
1class CommentsController < ApplicationController 2 def create 3 @problem = Problem.find(params[:comment][:problem_id]) 4 @comment = @problem.comments.new(comment_params) 5 @comment.customer_id = current_customer.id 6 @comment.score = Language.get_data(params[:comment][:body] ) 7 if@comment.save 8 redirect_to problem_path(@problem) 9 else 10 @comments = Comment.where(problem_id: @problem.id) 11 render template: "problems/show" 12 end 13 end
lib/language.rb
lib
1require 'base64' 2require 'json' 3require 'net/https' 4module Language 5 class << self 6 def get_data(text) 7 api_url = [ここにAPIkey書いてます] 8 # APIリクエスト用のJSONパラメータ 9 params = { 10 document: { 11 type: 'PLAIN_TEXT', 12 content: text 13 } 14 }.to_json 15 # Google Cloud Natural Language APIにリクエスト 16 uri = URI.parse(api_url) 17 https = Net::HTTP.new(uri.host, uri.port) 18 https.use_ssl = true 19 request = Net::HTTP::Post.new(uri.request_uri) 20 request['Content-Type'] = 'application/json' 21 response = https.request(request, params) 22 # APIレスポンス出力 23 pp request 24 pp params 25 pp response 26 pp response.body 27 JSON.parse(response.body)['documentSentiment']['score'] 28 end 29 end 30end 31
show
1 <h3>コメント</h3> 2 <% @comments.each do |comment| %> 3 <div class="comment-content"> 4 <span><%= attachment_image_tag comment.customer,:image, :fill,60,60 ,format: 'jpeg',fallback: "no_image.jpg" ,class: "customer-image" %></span> 5 <span><%= comment.created_at.strftime('%Y/%m/%d') %></span><br> 6 <span class="comment-body"><%= comment.body %></span> 7 <span id="likes_buttons_<%= comment.id %>"> 8 <%= render partial: 'likes/like', locals: { comment: comment} , class: 'like' %> 9 </span> 10 <% if comment.customer_id == current_customer.id %> 11 <span><%= link_to comment_path(comment), method: :delete do %></span> 12 <i class="far fa-trash-alt"></i> 13 <% end %> 14 <% end %> 15 <% if comment&.score.to_i >= 1.5 %> 16 <i class="fas fa-praying-hands"></i> 17 <td class="score_comment1">VeryGood</td> 18 <% elsif comment&.score.to_i <= 1.4 && comment&.score.to_i >= 0.7 %> 19 <td><i class="far fa-thumbs-up"></i></td> 20 <td class="score_comment2">good</td> 21 <% else %> 22 <td><i class="far fa-thumbs-down"></i></td> 23 <td class="score_comment3">Bad</td> 24 <% end %> 25 </div> 26 <% end %>
shema
1 create_table "comments", force: :cascade do |t| 2 t.integer "customer_id" 3 t.integer "problem_id" 4 t.text "body" 5 t.decimal "score", precision: 5, scale: 3 6 t.datetime "created_at", null: false 7 t.datetime "updated_at", null: false 8 end
あなたの回答
tips
プレビュー