2015-10-20 62 views
1

我正在使用我的Rails项目中的React Post系统。 但是,当我尝试呈现帖子在我的主页,我得到这个错误:Rails和React:TypeError:无法调用未定义的方法'map'

React::ServerRendering::PrerenderError in HomeController#index Encountered error "TypeError: Cannot call method 'map' of undefined" when prerendering PostsList with {"posts":null} React.createClass.render ((execjs):19669:38)

我的代码是上面:

posts_list.js.jsx: 
var PostsList = React.createClass({ 
    getInitialState: function() { 
     return { posts: this.props.initialPosts }; 
    }, 

    render: function() { 
     var posts = this.state.posts.map(function(post) { 
      return <Post key={post.id} post={post} />; 
     }); 

     return (
      <div className="posts"> 
       {posts} 
      </div> 
     ); 
    } 
}); 

post.js.jsx

var Post = React.createClass({ 
    render: function() { 
     return (
      <div className="post"> 
       <PostTitle post={this.props.post} /> 
       <PostBody post={this.props.post} /> 
      </div> 
     ); 
    } 
}); 

var PostTitle = React.createClass({ 
    render: function() { 
     return (
      <div className="post-header"> 
       <h2>{this.props.post.title}</h2> 
       <div className="post-meta"> 
        By {this.props.post.user_id} - {this.props.post.created_at} 
       </div> 
      </div> 
     ); 
    } 
}); 

var PostBody = React.createClass({ 
    render: function() { 
     return (
      <div className="post-contents"> 
       {this.props.post.body} 
      </div> 
     ); 
    } 
}); 

和index.html.erb

<div class="container-fluid"> 
    <div class="col-md-8"> 
     <h3> Posts Feed </h3> 
     <%= react_component('PostsList', {initialPosts: @posts }, {prerender: true}) %> 
    </div> 
</div> 

post_controller.rb

def index 
     @posts = Post.all 
     @user = current_user 
end 

当我使用{initialPosts:Post.All}其作品,但是当我用@posts使用,它不是

任何想法,为什么发生的呢?我已经发布到该网站的帖子和@posts不是空的。

+0

它不是这样工作..当我使用{initialPosts:Post.all}其作品,但否则不.. – pureofpure

回答

0

好吧,我发现什么是错的,我渲染反应在家庭布局,但@posts是在岗位控制器而不是家庭控制器定义。

相关问题