モデルの作成
[bash]
$ rails g model board title:string description:text
$ rails g model user name:string sex:integer age:integer
$ rails g model comment board_id:integer user_id:integer comment:text
[/bash]
app/assets/javascripts/components/App.js
[bash]
import React from “react”
import CBoardList from “../containers/CBoardList”
import CCommentAdd from “../containers/CCommentAdd”
import CCommentList from “../containers/CCommentList”
const App = ({}) => {
return (
)
}
export default App
[/bash]
app/assets/javascripts/containers/CBoardList.js
[bash]
import React from “react”
import {connect} from “react-redux”
import BoardList from “../components/BoardList”
app/assets/javascripts/containers/CCommentAdd.js
[bash]
import React from “react”
import {connect} from “react-redux”
import CommentAdd from “../components/CommentAdd”
app/assets/javascripts/containers/CCommentList.js
[bash]
import React from “react”
import {connect} from “react-redux”
import CommentList from “../components/CommentList”
app/assets/javascripts/components/CommentAdd.js
[bash]
import React from “react”
const CommentAdd = ({}) => {
return (
This is Comment Add Area
)
}
export default CommentAdd
[/bash]
app/assets/javascripts/components/CommentList.js
[bash]
import React from “react”
const CommentList = ({}) => {
return (
This is Comment List
)
}
export default CommentList
[/bash]
アプリケーションで利用するCSS作成
とりあえず全て貼り付けておきます。単純にコピペしてください。
app/assets/stylesheets/board.scss
[bash]
// Place all the styles related to the board controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
div.left-side{
float: left;
width: 400px;
}
app/assets/javascripts/reducers/root.js
[bash]
import {combineReducers} from “redux”
import sel_board from “./sel_board”
import comments from “./comments”
[bash]
class BoardController < ApplicationController
def index
@boards = Board.all.order("created_at DESC")
@users = User.all.order("id")
end
end
[/bash]
[bash]
import React, { Component } from ‘react’
import { render } from ‘react-dom’
import { Provider } from ‘react-redux’
import { createStore } from ‘redux’
import rootReducer from ‘./reducers/root’
import App from ‘./components/App’
const Board = ({boards, users}) => {
return (
)
}
export default Board
[/bash]
app/assets/javascripts/components/App.js
[bash]
import React from “react”
import CBoardList from “../containers/CBoardList”
import CCommentAdd from “../containers/CCommentAdd”
import CCommentList from “../containers/CCommentList”
const App = ({boards, users}) => {
return (
)
}
export default App
[/bash]
掲示板一覧のラッパー「CBoardList」、掲示板一覧「BoardList」を調整
app/assets/javascripts/containers/CBoardList.js
[bash]
import React from “react”
import {connect} from “react-redux”
import {board_select, comment_read} from “../actions/index”
import BoardList from “../components/BoardList”
app/assets/javascripts/components/BoardList.js
[bash]
import React from “react”
import BoardLine from “./BoardLine”
const BoardList = ({datas, selb, onBoardClick}) => {
let list = []
datas.map((b) => {
let active = (b.id == selb)
list.push( onBoardClick(b.id)}
/>)
})
return (
{list}
)
}
export default BoardList
[/bash]
掲示板の一行を表す「BoardLine」を作成
[bash]
import React from “react”
const BoardLine = ({id, title,active, onClick}) => {
let cssact = active ? “active” : “”
return (
)
}
export default BoardLine
[/bash]
ここまでで動作確認
これで最低限のところは出来たので、Railsサーバーを起動して、動作確認。
http://localhost:3000/board/index
こんな感じのイメージが表示されれば成功。
コメント投稿エリア作成
ラッパー「CCommentAdd」、投稿エリア本体「CommentAdd」を調整します。
app/assets/javascripts/containers/CCommentAdd.js
[bash]
import React from “react”
import {connect} from “react-redux”
import CommentAdd from “../components/CommentAdd”
import {comment_add, comment_read} from “../actions/index”
app/assets/javascripts/containers/CBoardList.js
[bash]
import React from “react”
import {connect} from “react-redux”
import {board_select, comment_read} from “../actions/index”
import BoardList from “../components/BoardList”
config/routes.rb
[bash]
Rails.application.routes.draw do
get ‘board/index’
get ‘board/readComments/:board_id’ => ‘board#readComments’
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
[/bash]
コントローラーの調整
app/controllers/board_controller.rb
[bash]
class BoardController < ApplicationController
def index
@boards = Board.all.order("created_at DESC")
@users = User.all.order("id")
end
def readComments
response = {status:true}
recs = Comment.eager_load(:user).where("board_id = ?", params[:board_id])
.order("comments.created_at DESC").as_json(include: :user)
response["rows"] = recs
render :json=> response
end
end
[/bash]
モデルの調整
app/models/comment.rb
[bash]
class Comment < ApplicationRecord
belongs_to :user
end
[/bash]
コメント一覧のラッパー、本体の調整
app/assets/javascripts/containers/CCommentList.js
[bash]
import React from “react”
import {connect} from “react-redux”
import CommentList from “../components/CommentList”
[bash]
Rails.application.routes.draw do
get ‘board/index’
post ‘board/insertComment’
get ‘board/readComments/:board_id’ => ‘board#readComments’
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
[/bash]
コメント投稿のラッパーと本体の調整
app/assets/javascripts/containers/CCommentAdd.js
[bash]
import React from “react”
import {connect} from “react-redux”
import CommentAdd from “../components/CommentAdd”
import {comment_add, comment_read} from “../actions/index”
Leave a comment