Ruby on Rails mein Tailwind CSS Use Karne ka Aasaan Tareeka

Ruby on Rails mein Tailwind CSS Use Karne ka Aasaan Tareeka

Namaskar developers! πŸ‘‹ Agar aap modern, responsive websites banana chahte hain aur Tailwind CSS ko apne Rails project mein integrate karna chahte hain, toh yeh guide aapke liye hai. Chaliye step-by-step seekhte hain:


1. Pehli Setting: Tailwind Setup Karo

Terminal mein yeh commands chalao:

# Rails project create karo (agar nahi hai toh)
rails new my_app -d postgresql

# Tailwind CSS ke liye official gem add karo
bundle add tailwindcss-rails

# Tailwind install karo
rails tailwindcss:install

βœ… Kya Hua?

  • tailwindcss-rails gem add hui
  • app/assets/stylesheets/application.tailwind.css file bani
  • bin/dev script update hui (foreman ke saath)

2. Important Files ko Samjho

application.tailwind.css (Tailwind ka entry point)

@tailwind base;
@tailwind components;
@tailwind utilities;

Procfile.dev (Automatic compilation ke liye)

web: bin/rails server -p 3000
css: bin/rails tailwindcss:watch

3. Views mein Tailwind Use Karo

Kisi bhi view file (e.g., app/views/home/index.html.erb) mein try karo:

<div class="bg-blue-500 text-white p-8 text-center">
  <h1 class="text-4xl font-bold">Swagat Hai!</h1>
  <p class="mt-4">Yeh Tailwind CSS ka magic hai πŸ”₯</p>
  <button class="mt-6 bg-yellow-400 hover:bg-yellow-500 px-6 py-2 rounded-full">
    Click Karo
  </button>
</div>

4. Server Start Karo

Tailwind ko automatic compile karne ke liye new command use karo:

bin/dev  # Regular 'rails s' se Tailwind compile nahi hoga!

Ab localhost:3000 visit karo – dekho Tailwind ka chamatkar! ✨


5. Customization (Theme Badlo)

config/tailwind.config.js file edit karke apna style define karo:

module.exports = {
  theme: {
    extend: {
      colors: {
        'mera-custom-color': '#ff00ff',
      }
    }
  }
}

CSS mein use karo:

<div class="bg-mera-custom-color">Mast rang!</div>

6. Production Build Taiyar Karo

Deploy se pehle CSS optimize karo:

rails tailwindcss:build

7. Common Problems & Solutions

Problem 1: Styles kaam nahi kar rahe

  • Solution: bin/dev se server start karo (regular rails s nahi)
  • Check karo application.html.erb mein yeh line hai:
  <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

Problem 2: Custom classes add karni hain

  • Solution: @layer use karo application.tailwind.css mein:
  @layer components {
    .mera-btn {
      @apply px-4 py-2 bg-red-500 rounded-lg;
    }
  }

Problem 3: PurgeCSS ko content dikhao

  • tailwind.config.js mein yeh ensure karo:
  content: [
    './app/views/**/*.html.erb',
    './app/helpers/**/*.rb',
    './app/assets/stylesheets/**/*.css',
    './app/javascript/**/*.js'
  ],

8. Pro Tips ✨

  1. Dev Tools: Browser DevTools mein Tailwind classes edit kar direct dekho changes
  2. Plugins:
   yarn add @tailwindcss/forms @tailwindcss/typography


tailwind.config.js mein add karo:

   plugins: [require('@tailwindcss/forms'), require('@tailwindcss/typography')]
  1. VSCode Extension: “Tailwind CSS IntelliSense” install karo for autocomplete

Final Structure

my_app/
β”œβ”€β”€ app/
β”‚   └── assets/
β”‚       └── stylesheets/
β”‚           └── application.tailwind.css  # Yeh hai hamari Tailwind entry
β”œβ”€β”€ config/
β”‚   └── tailwind.config.js  # Customizations yahan
└── Procfile.dev  # Foreman config

Aakhri Baat:
Tailwind CSS = “Utility-first” magic jo development speed rocket bana deta hai! πŸš€
“Pehle Bootstrap ka istemal karte the, ab Tailwind ki mohabbat ho gayi hai!” πŸ˜‰

Happy Coding! πŸ™Œ
bin/dev chalao, chai piyo, aur awesome UIs banao!” β˜•οΈπŸ’»

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *