2015-10-20 65 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是在崗位控制器而不是家庭控制器定義。

相關問題