###前提・実現したいこと
RailsアプリケーションにReactを導入し、その中でAjaxを利用しているのですが、GETのリクエスト時にエラーが発生しているので、それを解消したいと思っています。
テキストをマイクロポストとして投稿できるWEBサービスを開発しています。
Rails 4.2.2
Ruby 2.0.0
で開発をしています。
###発生している問題・エラーメッセージ・コード
ajaxを記載しているmicropost.js.jsx内の下記の部分でエラーが発生しているように見えます。
(ulrの111の部分は、動的なマイクロポストのidに後々変更する予定です)
componentDidMount: function() { $.ajax({ url: 'microposts/111', dataType: 'json', type: "GET", cache: false, success: function(messages) { this.setState({ messages: messages, isLoading: false }); }.bind(this), error: function(_xhr, status, err) { console.error(this.props.url, status, err.toString()); }.bind(this) }); },
サーバーログを確認すると、下記のようなInternal Server errorが発生しています。
Started GET "/microposts/111?_=1484144300171" for ::1 at 2017-01-11 23:18:20 +0900 Processing by MicropostsController#show as JSON Parameters: {"_"=>"1484144300171", "id"=>"111"} Micropost Load (0.9ms) SELECT "microposts".* FROM "microposts" WHERE "id"."id" = '111' ORDER BY "microposts"."created_at" DESC LIMIT 1 SQLite3::SQLException: no such column: id.id: SELECT "microposts".* FROM "microposts" WHERE "id"."id" = '111' ORDER BY "microposts"."created_at" DESC LIMIT 1 Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.1ms) SQLite3::SQLException (no such column: id.id): app/controllers/microposts_controller.rb:23:in `show'
対応するmicroposts_controller.rbのコードは下記のようになっています。
showアクションを今回のajaxでは利用しています。
class MicropostsController < ApplicationController before_action :logged_in_user, only: [:create, :destroy] before_action :set_micropost, only: :destroy def create @micropost = current_user.microposts.build(micropost_params) if @micropost.save flash[:success] = "Micropost created!" redirect_to home_path else @feed_items = [] render 'static_pages/home' end end def destroy @micropost.destroy flash[:success] = "Micropost deleted" redirect_to request.referrer || home_path end def show @micropost = Micropost.find(id: params[:id]) end private def micropost_params params.require(:micropost).permit(:content, :picture) end def set_micropost @micropost = current_user.microposts.find_by(id: params[:id]) redirect_to home_path if @micropost.nil? end end
###試したこと
ログを見ると
Parameters: {"_"=>"1484144300171", "id"=>"111"}
と表記されているのですが、本来であればparameterにはidだけ渡されるのが正しいはずなので、"_"の出処を調べたのですが、どこから渡されているのか突き止められませんでした。。
"_"リクエストのたびに作り変えられているようでした。
その他、必要な情報などあればおっしゃっていただければと思います。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。