###前提・実現したいこと
rails4で、ツイッターアカウントを通してイベントを作成し、それに参加できるというアプリを作成しています。connpassみたいなアプリです。
今、参加するときにコメントを必要とする画面で、もし30字以上のコメントを要求した場合、エラーメッセージを出すようにjsonでcoffeeに渡しているのですがうまくいきません。回答よろしくお願いします。
###発生している問題・エラーメッセージ
POST http://localhost:3000/events/8/tickets 422 tickets.self-f0a7c9b….js?body=1:11 Uncaught ReferenceError: message is not defined
###該当のソースコード
ruby
1app/controllers/TicketsController 2class TicketsController < ApplicationController 3 before_action :authenticate 4 5 def new 6 raise ActionController::RoutingError,'ログイン状態で TicketsController#new にアクセス' 7 end 8 9 def create 10 ticket = current_user.tickets.build do |t| 11 t.event_id = params[:event_id] 12 t.comment = params[:ticket][:comment] 13 end 14 if ticket.save 15 flash[:notice] = 'このイベントに参加表明をしました。' 16 head 201 17 else 18 render json: { messages: ticket.errors.full_messages }, status: 422 19 end 20 end 21 22 23end 24 25asset/javascript/tickets.js.coffee 26# Place all the behaviors and hooks related to the matching controller here. 27# All this logic will automatically be available in application.js. 28# You can use CoffeeScript in this file: http://coffeescript.org/ 29 30$(document).on 'ajax:success','#createTicket',(xhr,data,status) -> 31 location.reload() 32 33$(document).on 'ajax:error','#createTicket',(xhr,data,status) -> 34 form = $('#new_ticket .modal-body') 35 div = $('<div id="createTicketErrors" class="alert alert-danger"></div>') 36 ul = $('<ul></ul>') 37 data.responseJSON.messages.forEach(message, i) -> 38 li = $('<li></li>').text(message) 39 ul.append(li) 40 41 42 if $('#createTicketErrors')[0] 43 $('#createTicketErrors').html(ul) 44 else 45 div.append(ul) 46 form.prepend(div) 47
###試したこと
30字以内というvalidatesはticketのモデルの記述なので、そこを確認したが不審な点はなく。普通に30字以内ならば通るので、上の二つのファイル以外は大丈夫なのか、と思っています。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。