質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

997閲覧

開発環境では動くのですが本番環境で動きません

morfonica

総合スコア33

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/09/02 13:39

編集2021/09/04 01:26

前提・実現したいこと

Ruby on Railsでwebアプリを作っています。

Google Natural Language API (自然言語処理)を実装中に以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

We're sorry, but something went wrong. If you are the application owner check the logs for more information.
I, [2021-09-04T01:12:45.890639 #10933] INFO -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] Started POST "/posts" for 60.74.201.90 at 2021-09-04 01:12:45 +0000 I, [2021-09-04T01:12:45.891323 #10933] INFO -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] Processing by Public::PostsController#create as HTML I, [2021-09-04T01:12:45.891378 #10933] INFO -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] Parameters: {"utf8"=>"✓", "authenticity_token"=>"d+GyHP7Y+CO8M4JloINAIgiGvJmgQv+CMtIGpBlH/KB8NAJOHAnEgcjNzGAbswk/3NsNw20QpknB9/zGH/d0NQ==", "post"=>{"text"=>"test", "category_id"=>"2"}, "commit"=>"投稿する"} D, [2021-09-04T01:12:45.897002 #10933] DEBUG -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] Customer Load (2.3ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 ORDER BY `customers`.`id` ASC LIMIT 1 I, [2021-09-04T01:12:46.737837 #10933] INFO -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] Completed 500 Internal Server Error in 846ms (ActiveRecord: 2.3ms) F, [2021-09-04T01:12:46.738180 #10933] FATAL -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] F, [2021-09-04T01:12:46.738215 #10933] FATAL -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] RuntimeError (The request is missing a valid API key.): F, [2021-09-04T01:12:46.738237 #10933] FATAL -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] F, [2021-09-04T01:12:46.738260 #10933] FATAL -- : [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] lib/language.rb:27:in `get_data' [7344f60e-ad1a-4347-80d0-9fe1ef4d31c8] app/controllers/public/posts_controller.rb:42:in `create'

該当のソースコード

lib/language.rb

1require 'base64' 2require 'json' 3require 'net/https' 4 5module Language 6 class << self 7 def get_data(text) 8 # APIのURL作成 9 api_url = "https://language.googleapis.com/v1beta1/documents:analyzeSentiment?key=#{ENV['GOOGLE_API_KEY']}" 10 # APIリクエスト用のJSONパラメータ 11 params = { 12 document: { 13 type: 'PLAIN_TEXT', 14 content: text 15 } 16 }.to_json 17 # Google Cloud Natural Language APIにリクエスト 18 uri = URI.parse(api_url) 19 https = Net::HTTP.new(uri.host, uri.port) 20 https.use_ssl = true 21 request = Net::HTTP::Post.new(uri.request_uri) 22 request['Content-Type'] = 'application/json' 23 response = https.request(request, params) 24 # APIレスポンス出力 25 response_body = JSON.parse(response.body) 26 if (error = response_body['error']).present? 27 raise error['message'] 28 else 29 response_body['documentSentiment']['score'] 30 end 31 end 32 end 33end

posts.controller

1class Public::PostsController < ApplicationController 2 3 before_action :authenticate_customer! 4 before_action :ensure_correct_customer, only: [:edit, :update, :destroy] 5 6 def index 7 if params[:category_id] 8 # カテゴリー検索したら 9 @category = Category.find(params[:category_id]) 10 all_posts = @category.posts 11 else 12 all_posts = Post.includes(:category) 13 end 14 muted_customer = Relationship.where(muter_id: current_customer.id) 15 # 共感数が多い順 16 if params[:sort] == "sympathy" 17 @posts = all_posts.page(params[:page]).left_outer_joins(:sympathies).where.not(customer_id: muted_customer.pluck(:muted_id)). 18 group('posts.id').select('posts.*, COUNT("sympathies.*") AS sympathy').order('count(post_id) desc') 19 # 結合したテーブルをpost.idでグループ化する。共感されている対象のPostが同じ同士でグループ化する 20 # select文で返すデータを指定(postテーブルの全てとsympathies_count) 21 # pluck:activeモデルを継承していないと使えない。配列[]から指定したカラムを持ってくる。無いとnullになる。 22 # 応援数が多い順 23 elsif params[:sort] == "cheer" 24 @posts = all_posts.page(params[:page]).left_outer_joins(:cheers).where.not(customer_id: muted_customer.pluck(:muted_id)). 25 group('posts.id').select('posts.*, COUNT("cheers.*") AS cheer').order('count(post_id) desc') 26 else 27 @posts = all_posts.page(params[:page]).reverse_order.where.not(customer_id: muted_customer.pluck(:muted_id)) 28 end 29 @all_posts_count = all_posts.count 30 @categories = Category.all 31 end 32 33 def new 34 @post = Post.new 35 @categories = Category.all 36 end 37 38 def create 39 post = Post.new(post_params) 40 post.customer_id = current_customer.id 41 42 # API側から返ってきた値をもとにスコアを作成 43 post.score = Language.get_data(post_params[:text]) 44 45 if post.save! 46 redirect_to mypage_path, notice: '投稿しました!' 47 else 48 @categories = Category.all 49 render :new 50 end 51 52 end 53 54 def edit 55 end 56 57 def update 58 if @post.update(post_params) 59 redirect_to mypage_path, notice: '更新しました!' 60 else 61 render :edit 62 end 63 end 64 65 def destroy 66 @post.destroy 67 redirect_to posts_path 68 end 69 70 def search 71 @posts = Post.where('text LIKE(?)', "%#{params[:keyword]}%") 72 render :index 73 end 74 75 private 76 77 def post_params 78 params.require(:post).permit(:text, :category_id, :sympathies, :cheers, :image) 79 end 80 81 def ensure_correct_customer 82 @post = Post.find(params[:id]) 83 unless @post.customer == current_customer 84 redirect_to posts_path 85 end 86 end 87end 88

試したこと

データベースのリセット
$ RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rails db:drop
gemfileの変更
$ bundle install --path vendor/bundle --without test development
css/JavaScript/画像の変更
$ bundle exec rails assets:precompile RAILS_ENV=production
マイグレーションファイルの変更
$ bundle exec rails db:migrate RAILS_ENV=production
は行いました。

補足情報(FW/ツールのバージョンなど)

cloud9
rails version 5.2.6

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yu_1985

2021/09/02 16:26

本当にログの内容はそれだけでしょうか? 端折っているのであればもう少し広範囲でログを見てください。
yu_1985

2021/09/05 16:22

The request is missing a valid API key と出てますが、ちゃんと環境変数は設定できてますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問