使用Rails反应JS - 使用URL访问JSON数据

时间:2022-04-17 15:55:10

I'm learning how to use React JS with my Rails app. Disclaimer: This is the first time I am messing around with JS!

我正在学习如何在我的Rails应用程序中使用React JS。免责声明:这是我第一次搞乱JS!

I'm following a talk by Michael Chan at Rails Conf. Everything works beautifully until I hit the CommentsContainer part, where he fetches the comments data from a Rails route. I am trying to the same thing, except on a simple Songs controller (generated with a scaffold, having name and artist as the attribute).

我跟随Michael Chan在Rails Conf的演讲。一切都运行得很漂亮,直到我点击CommentsContainer部分,在那里他从Rails路线获取评论数据。我试图做同样的事情,除了在一个简单的歌曲控制器(用脚手架生成,名称和艺术家作为属性)。

I am getting stuck at the part where he fetches this data with the path, I end up with a 500 error page when I try to run the code. I don't know how to debug my React code, since the browser console is just showing a 500 error.

我陷入了他用路径获取此数据的部分,当我尝试运行代码时,我最终得到了一个500错误页面。我不知道如何调试我的React代码,因为浏览器控制台只显示500错误。

Here's my view,

这是我的看法,

<%= react_component "SongsContainer", { songsPath: songs_path } %>

And here's my React code,

这是我的React代码,

var SongsContainer = React.createClass({
componentWillMount(){
    this.fetchSongs();
    setInterval(this.fetchSongs, 1000);
},

fetchSongs() {
    $.getJSON(
        this.props.songsPath,
        (data) => this.setState({songs: data});
    );
},

getInitialState() {
    return { songs: [] };
},

render() {
    return <Songs songs={this.state.songs} />;
}
});

Any help would greatly be appreciated! I'm just looking at this 500 page not knowing what to do.

任何帮助将不胜感激!我只是看着这500页不知道该怎么做。

EDIT: Adding Server log

编辑:添加服务器日志

Started GET "/" for ::1 at 2015-08-11 18:19:30 +0530
Processing by SongsController#index as HTML
  Rendered songs/index.html.erb within layouts/application (0.6ms)
Completed 500 Internal Server Error in 373ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (SyntaxError: unknown: Unexpected token (10:41)):
    3: <head>
    4:   <title>Albums</title>
    5:   <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    6:   <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    7:   <%= csrf_meta_tags %>
    8: </head>
    9: <body>
  app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___3071413699732960348_70238909518380'

1 个解决方案

#1


0  

Finally after lots of struggle, I found a solution!

经过多次努力,我找到了解决方案!

  1. Remove Turbolinks from application.html.
  2. 从application.html中删除Turbolinks。

  3. Replace the $.getJSON function with this,

    用这个替换$ .getJSON函数,

    $.ajax({ url: this.props.songsPath, dataType: 'json', success: function(data) { this.setState({songs: data}); }.bind(this) });

    $ .ajax({url:this.props.songsPath,dataType:'json',success:function(data){this.setState({songs:data});} .bind(this)});

Now, I really don't know much about JS, so I don't know why this works, but it just does! Cheers.

现在,我真的不太了解JS,所以我不知道为什么会这样,但它确实如此!干杯。

#1


0  

Finally after lots of struggle, I found a solution!

经过多次努力,我找到了解决方案!

  1. Remove Turbolinks from application.html.
  2. 从application.html中删除Turbolinks。

  3. Replace the $.getJSON function with this,

    用这个替换$ .getJSON函数,

    $.ajax({ url: this.props.songsPath, dataType: 'json', success: function(data) { this.setState({songs: data}); }.bind(this) });

    $ .ajax({url:this.props.songsPath,dataType:'json',success:function(data){this.setState({songs:data});} .bind(this)});

Now, I really don't know much about JS, so I don't know why this works, but it just does! Cheers.

现在,我真的不太了解JS,所以我不知道为什么会这样,但它确实如此!干杯。