【问题标题】:react-rails with jbuilder doesn't work使用 jbuilder 的 react-rails 不起作用
【发布时间】:2015-06-11 21:25:25
【问题描述】:

我尝试将react-rails 与 jbuilder 一起使用。 例如,我使用this great sample app of react-rails 并尝试使用来自this part 的引用在react-rails 中重写以使用jbuilder 作为json 响应。

这些是主要的重写部分:

# 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'

您有解决此错误的想法吗? 提前致谢。

查看完整源代码: Use jbuilder but it gets an error · jwako/sample-react-rails-app@508d581

【问题讨论】:

    标签: ruby-on-rails reactjs jbuilder react-rails


    【解决方案1】:

    似乎引发了意外的令牌,因为当 this.props.presenter 已经是 JSON 对象时,CommentBox 组件正在调用 JSON.parse() in getInitialState

    您可以通过删除getInitialState 中的JSON.parse() 并仅返回this.props.presenter 来修复它。

    这是解析现有 JSON 对象的相关answer

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 2012-10-03
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多