What i want to do ?
I wan't to show on my rails view files data with react.js component from my rails models.
What i have ?
I have installed these gems
rails-admin
react-rails
What i did ?
I created a new rails project. I installed react-rails gem and rails-admin gem created new controller with following types
rails g scaffold Post title:string body:text
and i can add from rails admin posts all is okey.
my controller looking like :
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
@post = Post.find(params[:id])
end
# GET /posts/new
def new
@post = Post.new
end
and here is my views/posts/index.html.erb file
<%= react_component('PostList', {name: raw(render(template: 'posts/index', formats: :json)) }) %>
here is views/posts/index.json.jbuilder file
json.array!(@posts) do |post|
json.extract! post, :id, :title, :body
json.url post_url(post, format: :json)
end
here is views/posts/show.json.jbuilder file
json.extract! @post, :id, :title, :body, :created_at, :updated_at
here is my react component file assets/javascripts/components/post_list.js.jsx
var PostList = React.createClass({
render: function() {
return (
<div>
{JSON.parse(this.props.posts).map(function(post) {
return <Post key={post.id} {... post} />;
})}
</div>
);
}
});
So i can't understand where is i'm wrong. I can't get data from json to index.html.erb. How can i do that? Please help i'm stuck and i can't find nothing understandable on internet