1

react-railsはjbuilderで使用しようとします。たとえば、私はこのreact-railsの素晴らしいサンプルアプリを使用し、JSON応答としてjbuilderを使用するように書き直そうとしましreact-rails.

主な書き換え部分は次のとおりです。

# app/views/comments/index.html.erb
<%= react_component 'CommentBox', render(template: 'comments/index.json.jbuilder'), {prerender: true} %>
# app/views/comments/index.json.jbuilder
json.presenter do
  json.comments(@comments) do |comment|
    json.extract! comment, :id, :author
  end
  json.form do
    json.action comments_path
    json.csrf_param request_forgery_protection_token
    json.csrf_token form_authenticity_token
  end
end
json.imgSrc image_path("gundam.jpg")

しかし、次のエラーが発生しました。

Started GET "/comments" for 127.0.0.1 at 2015-04-07 18:26:40 +0900
Processing by CommentsController#index as HTML
  Comment Load (0.9ms)  SELECT  "comments".* FROM "comments"   ORDER BY "comments"."id" DESC LIMIT 5
  Rendered comments/index.json.jbuilder (8.4ms)
  Rendered comments/index.html.erb within layouts/application (34.6ms)
Completed 500 Internal Server Error in 43ms

ActionView::Template::Error (SyntaxError: Unexpected token o):
  app/views/comments/index.html.erb:3:in `_app_views_comments_index_html_erb___3472795088323540071_70123597415980'

このエラーを解決するアイデアはありますか? 前もって感謝します。

完全なソース コードを表示: jbuilder を使用するとエラーが発生する · jwako/sample-react-rails-app@508d581

4

1 に答える 1

2

がすでに JSON オブジェクトであるときにCommentBox コンポーネントが呼び出さJSON.parse()れているため、予期しないトークンが発生したようです。getInitialStatethis.props.presenter

を削除JSON.parse()getInitialStateて戻すだけで修正できますthis.props.presenter

これは、既存の JSON オブジェクトの解析に関する関連する回答です。

于 2015-04-16T20:09:26.557 に答える