前提・実現したいこと
railsをサーバ、swiftをクライアントにした簡単なチャットアプリを開発しています。
JSONapiを利用した通信で、チャットのメッセージの内容がrailsサーバにポストできない(rejectされる)問題が発生しています。
発生している問題・エラーメッセージ
responseJSONをresponseStringにした時の結果↓
swift
1[Result]: SUCCESS: <!DOCTYPE html> 2<html> 3<head> 4 <title>We're sorry, but something went wrong (500)</title> 5 <meta name="viewport" content="width=device-width,initial-scale=1"> 6 <style> 7 //略 8 </style> 9</head> 10 11<body class="rails-default-error-page"> 12 <!-- This file lives in public/500.html --> 13 <div class="dialog"> 14 <div> 15 <h1>We're sorry, but something went wrong.</h1> 16 </div> 17 <p>If you are the application owner check the logs for more information.</p> 18 </div> 19</body> 20</html> 21
該当のソースコード
swift
1override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) { 2 3 //キーボードを閉じる 4 self.view.endEditing(true) 5 6 //メッセージを追加 7 let message = JSQMessage(senderId: senderId, displayName: senderDisplayName, text: text) 8 self.messages.append(message!) 9 10 let parameters: Parameters = [ 11 "text": message!.text 12 ] 13 14 Alamofire.request("https://floating-wildwood-37299.herokuapp.com/api/messages", 15 method: .post, 16 parameters: parameters, 17 encoding: JSONEncoding.default).responseString { response in 18 debugPrint(response) 19 } 20 21 //送信を反映 22 self.finishReceivingMessage(animated: true) 23 24 //textFieldをクリアする 25 self.inputToolbar.contentView.textView.text = "" 26 }
rake routes の結果、該当部分
rails
1api_messages GET /api/messages(.:format) api/messages#index 2 POST /api/messages(.:format) api/messages#create
APIのコントローラー
class Api::MessagesController < ApplicationController def index //・・・略 end def create @userid = User.count @messageid = Message.count + 1 Message.create!(id: "#{@messageid}", content: "#{create_params}", user_id: "#{@userid}") end private def create_params params.require.permit(:text) end end
herokuのlog
heroku[web.1]: State changed from starting to up app[web.1]: I, [2018-05-07T03:54:46.083453 #10] INFO -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] Started POST "/api/messages" for 111.102.179.225 at 2018-05-07 03:54:46 +0000 app[web.1]: I, [2018-05-07T03:54:46.175587 #10] INFO -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] Processing by Api::MessagesController#create as */* app[web.1]: I, [2018-05-07T03:54:46.175751 #10] INFO -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] Parameters: {"text"=>"Gkdddddddd", "message"=>{}} app[web.1]: D, [2018-05-07T03:54:46.203983 #10] DEBUG -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] (1.8ms) SELECT COUNT(*) FROM "users" app[web.1]: D, [2018-05-07T03:54:46.212653 #10] DEBUG -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] (1.6ms) SELECT COUNT(*) FROM "messages" app[web.1]: I, [2018-05-07T03:54:46.215776 #10] INFO -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] Completed 500 Internal Server Error in 40ms (ActiveRecord: 27.3ms) app[web.1]: F, [2018-05-07T03:54:46.218188 #10] FATAL -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] app[web.1]: F, [2018-05-07T03:54:46.221196 #10] FATAL -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] ArgumentError (wrong number of arguments (given 0, expected 1)): app[web.1]: F, [2018-05-07T03:54:46.221323 #10] FATAL -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] app[web.1]: F, [2018-05-07T03:54:46.221439 #10] FATAL -- : [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] app/controllers/api/messages_controller.rb:23:in `create_params' app[web.1]: [e6b4a80a-3c75-437a-81af-dcdb5b6180d3] app/controllers/api/messages_controller.rb:18:in `create' heroku[router]: at=info method=POST path="/api/messages" host=floating-wildwood-37299.herokuapp.com request_id=e6b4a80a-3c75-437a-81af-dcdb5b6180d3 fwd="111.102.179.225" dyno=web.1 connect=0ms service=152ms status=500 bytes=1891 protocol=https
補足情報(FW/ツールのバージョンなど)
ruby '2.4.1'
rails '5.1.4'
swift 4.1
pod 'Alamofire', '> 4.7'> 4.0'
pod 'SwiftyJSON', '
pod 'JSQMessagesViewController'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/07 07:19 編集