Rails 4 + ReactJS: “SyntaxError: cannot have an im

2019-05-18 18:07发布

I am following Fernando Villalobos' React.js - A guide for Rails developers AirPair tutorial.

The goal here is to build a simple expense tracking app, using Rails and React JS.

In the Nesting Components: Listing Records section, the author recommends to create a app/views/records/index.html.erb file as follows:

<%# app/views/records/index.html.erb %>
<%= react_component 'Records', { data: @records } %>

AND a javascripts/components/records.js.coffee file as follows:

# app/assets/javascripts/components/records.js.coffee

  @Records = React.createClass
    render: ->
      React.DOM.div
        className: 'records'
        React.DOM.h2
          className: 'title'
          'Records'

Then, when we visit localhost:3000/records, we are supposed to see the following:

enter image description here

However, when I visit localhost:3000/records, I get the following error:

ExecJS::RuntimeError in Records#index
SyntaxError: [stdin]:7:10: cannot have an implicit value in an implicit object
<title>Expenses</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

In particular, the line causing the issue seems to be:

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

Any idea of what the probelm is and how I can make things work?

2条回答
祖国的老花朵
2楼-- · 2019-05-18 18:48

Try the following coffescript instead of your current coffescript(Just removing React.DOM from the coffescript):

@Records = React.createClass
  render: ->
    div
      className: 'records'
      h2
        className: 'title'
        'Records'
查看更多
Summer. ? 凉城
3楼-- · 2019-05-18 18:58

My mistake.

I was using Ruby "two-space" indentation in my CoffeeScript file, which caused the error.

When I copied and pasted the code from the tutorial, with the right indentation, everything worked like a charm.

查看更多
登录 后发表回答