Last active
September 7, 2016 17:29
-
-
Save fuermosi777/cebc6227d00d45af59a041b844ad51d7 to your computer and use it in GitHub Desktop.
Very basic Ruby - React app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"presets": ["es2015", "react", "stage-0"], | |
"plugins": ["react-hot-loader/babel"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# base_controller.rb | |
class Api::V1::BaseController < ApplicationController | |
protect_from_forgery with: :null_session | |
respond_to :json | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails new someapp | |
$ rails g model Item name:string description:text | |
$ rake db:migrate | |
# gemfile | |
gem 'responders' | |
$ bundle | |
# react part | |
$ bin/rails generate controller React index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/entry.js | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import App from './components/App/App.jsx'; | |
ReactDOM.render(( | |
<App/> | |
), document.getElementById('container') | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Test</title> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" /> | |
</head> | |
<body> | |
<div id="container"></div> | |
</body> | |
<script src="http://localhost:8080/webpack-dev-server.js"></script> | |
<script src="http://localhost:8080/bundle.js"></script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#items_controller.rb | |
class Api::V1::ItemsController < Api::V1::BaseController | |
def index | |
respond_with Item.all | |
end | |
def create | |
respond_with :api, :v1, Item.create(item_params) | |
end | |
def destroy | |
respond_with Item.destroy(params[:id]) | |
end | |
def update | |
item = Item.find(params["id"]) | |
item.update_attributes(item_params) | |
respond_with item, json: item | |
end | |
private | |
def item_params | |
params.require(:item).permit(:id, :name, :description) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
"scripts": { | |
"dev": "webpack-dev-server --config webpack.config.dev.js --hot --inline --colors --progress", | |
"build": "webpack --config webpack.config.prod.js -p --progress --colors" | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm init | |
$ npm install --save-dev babel-loader babel-core webpack css-loader less-loader style-loader react react-dom babel-preset-react babel-preset-stage-0 babel-preset-es2015 react-hot-loader webpack-dev-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
get 'react/index' | |
root 'react#index' | |
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | |
namespace :api do | |
namespace :v1 do | |
resources :items, only: [:index, :create, :destroy, :update] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/seeds.rb | |
10.times { Item.create(name: "Hao", description: "Liu") } | |
# run $ rake db:seed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global __dirname */ | |
var webpack = require('webpack'); | |
module.exports = { | |
entry: [ | |
'webpack/hot/only-dev-server', | |
'./src/entry.jsx' | |
], | |
output: { | |
publicPath: 'http://localhost:8080/static/', | |
filename: 'bundle.js' | |
}, | |
module: { | |
loaders: [{ | |
test: /\.jsx$/, | |
loaders: ['babel'], | |
exclude: /node_modules/ | |
}, { | |
test: /\.js$/, | |
exclude: /node_modules/, | |
loader: 'babel' | |
}, { | |
test: /\.less$/, | |
loader: 'style!css!less' | |
}, { | |
test: /\.(css)$/, | |
loader: 'style!css' | |
}, { | |
test: /\.(png|jpg|jpeg|svg)$/, | |
loader: 'file' | |
}] | |
}, | |
plugins: [ | |
new webpack.HotModuleReplacementPlugin(), | |
new webpack.NoErrorsPlugin() | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment