Coder Social home page Coder Social logo

mango-rails's Introduction

Rails with React + Tailwindcss

An ambitious crossover that nobody asked for. Feel free to clone and test the repo:

git clone https://github.com/lnfel/rails-react-tailwind.git

see live sample at Heroku


Steps to re-create the project

Create new rails app:

$ rails new my-app
$ cd my-app

Add webpacker and react-rails to your gemfile:

gem 'webpacker'
gem 'react-rails'

Note: on Rails 6.x webpacker is already included.

Now run the installers:

$ bundle install
$ rails webpacker:install
$ rails webpacker:install:react
$ rails generate react:install

Generate your first component:

$ rails g react:component HelloWorld greeting:string

Generate controller and a view:

$ rails generate controller Site index

Render it in a Rails view:

<!-- app/views/site/index.html.erb -->

<%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>

Set route:

<!-- config/routes.rb -->

root 'site#index'

Let's start the app:

$ rails s

Output: greeting: "Hello from react-rails", inspect webpage in your browser too see change in tag props.

Make the app production ready:

move sqlite3 gem inside development and test group

<!-- Gemfile -->

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Put sqlite3 here
  gem 'sqlite3', '1.4.2'
end

then add pg gem

gem 'pg'

change production settings

<!-- config/database.yml -->

production:
 adapter: postgresql
 database_url: <%= ENV['DATABASE_URL'] %>
 secret_key_base: <%= ENV['SECRET_KEY_BASE'] %>
 rails_master_key: <%= ENV['RAILS_MASTER_KEY'] %>
 encoding: unicode
 pool: 5

Push the repo to Github and deploy the app to Heroku.

If you only need react for rails, you're good to go. To integrate tailwind for your app, follow the next steps

Integrating Tailwincss

Intall tailwind using yarn:

$ yarn add tailwindcss

Include tailwindcss in postcss.config.js:

<!-- postcss.config.js -->

module.exports = {
 plugins: [
   // ...
   require('tailwindcss')('./app/javascript/stylesheets/tailwind.js'),
   require('autoprefixer'),
   // ...
 ]
}

Setup tailwind for rails:

Create a folder named "stylesheets" in app/javascript then create "application.scss" inside stylesheets folder

<!-- app/javascript/stylesheets/application.scss -->

@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

Load application.scss:

<!-- app/javascript/packs/application.js -->

require("stylesheets/application.scss")

Change stylesheet_link_tag to stylesheet_pack_tag

<!-- app/views/layouts/application.html.erb -->

<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>

Create a file named "tailwind.config.js" in app/javascript/stylesheets

then paste this code:

module.exports = {
  theme: {
    extend: {}
  },
  variants: {},
  plugins: []
}

Try using tailwind utility classes on app/views/site/index.html.erb

Finally run the app:

$ rails s

Push the repo to Github and deploy the app to Heroku.

mango-rails's People

Contributors

lnfel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.