Easily Manage WordPress DB From Your Rails App

0
85

Watch tutorial pt 1 here:

database.yml

default: &default
adapter: sqlite3
pool: <%= ENV["PASSWORD"] %>
timeout: 5000

production:
adapter: mysql2
database: your-database-name
username: your-database-username
password: <%= ENV["PASSWORD"] %>
host: your-host-link
port: your-port

Gemfile

gem 'mysql2'

/config/application.yml

PASSWORD: your-database-password

 

.gitignore</strong

/config/application.yml

wp_post.rb

establish_connection(:production)
self.table_name = "wp_posts"

 

wp_posts_controller.rb

class WpPostsController < ApplicationController

before_action :set_wp_post, only: [:show, :edit, :update, :destroy] 

def index 
@wp_posts = WpPost.where(:post_type => 'post')
end

def show
@wp_post = WpPost.find(params[:id])
end

def edit
@wp_post = WpPost.find(params[:id])
end

def new
@wp_post = WpPost.new
@date = Time.now.strftime("%Y-%m-%d %H:%M:%S")
end

# POST /sortings
# POST /sortings.json
def create
@wp_post = WpPost.new(wp_post_params)

respond_to do |format|
if @wp_post.save
format.html { redirect_to wp_posts_path, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @wp_post }
else
format.html { render :new }
format.json { render json: @wp_post.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /sortings/1
# PATCH/PUT /sortings/1.json
def update
respond_to do |format|
if @wp_post.update(wp_post_params)
format.html { redirect_to @wp_post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @wp_post }
else
format.html { render :edit }
format.json { render json: @wp_post.errors, status: :unprocessable_entity }
end
end
end

def destroy
@wp_post.destroy
respond_to do |format|
format.html { redirect_to wp_posts_path, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end

private

def set_wp_post
@wp_post = WpPost.find(params[:id])
end

def wp_post_params
params.require(:wp_post).permit(:id, :post_author, :post_date, :post_date_gmt, :post_modified, :post_modified_gmt, :post_type, :post_content, :post_title, :post_status, :post_name, :post_excerpt, :comment_status, :ping_status, :to_ping, :pinged, :menu_order, :post_content_filtered, :guid, :post_mime_type, :post_id)
end

end

 

routes.rb

root to: 'wp_posts#index'
get '/wp_posts', to: 'wp_posts#wp_posts'
get '/wp_posts/new', to: 'wp_posts#new'
get '/wp_posts/:id', to: 'wp_posts#show'