Skip to content

Instantly share code, notes, and snippets.

View hpjaj's full-sized avatar

Harry Levine hpjaj

View GitHub Profile
// app/javascript/components/NewPost.js
render() {
return (
<div>
<h1>New Post</h1>
<form>
<input
name="title"
placeholder="title"
# config/routes.rb
namespace :api, defaults: { format: 'json' } do
resources :posts, only: [:index, :create]
end
# app/controllers/api/posts_controller.rb
def create
render json: { params: params }
end
// app/javascript/components/Posts.js
import React from 'react'
import axios from 'axios'
class Posts extends React.Component {
state = {
posts: []
};
// app/javascript/components/Posts.js
...
renderAllPosts = () => {
return(
<ul>
{this.state.posts.map(post => (
<li key={post}>{post}</li>
))}
</ul>
// app/javascript/components/Posts.js
...
class Posts extends React.Component {
state = {
posts: []
};
componentDidMount() {
axios
// app/javascript/components/Posts.js
import axios from 'axios'
...
# config/routes.rb
Rails.application.routes.draw do
root 'pages#index'
namespace :api, defaults: { format: 'json' } do
resources :posts, only: :index
end
# IMPORTANT #
# app/controllers/api/posts_controller.rb
module API
class PostsController < ApplicationController
def index
posts = ['Post 1', 'Post 2']
render json: { posts: posts }
end
end
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end