Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get tailwindcss grid to work in a Rails 7 project generated with --css tailwind flag

So I generated a brand new Rails 7 project with Tailwindcss and Postgres

rails new project -d postgresql --css tailwind

then I scaffolded a task model, which generated great looking css. Then I ventured to adapt the index page a bit, as the scaffold html/css drew it out vertically. I'm striving for a more horizontal layout, where individual tasks would form individual rows.

According to tailwind documentation I could use a grid for each task. As I have three attributes in a task I wanted to use a three column grid.

So I added grid gap-4 grid-cols-3 to my _task.html.haml

.grid.gap-4.grid-cols-3{:id => "#{dom_id task}"}
  .my-5
    %strong.block.font-medium.mb-1 Description:
    = task.description
  .my-5
    %strong.block.font-medium.mb-1 Notes:
    = task.notes
  .my-5
    %strong.block.font-medium.mb-1 Deadline:
    = task.deadline
  - if action_name != "show"
    = link_to "Show this task", task, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium"
    = link_to 'Edit this task', edit_task_path(task), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium"
    %hr.mt-6

But it continues to render the attributes below each other.

enter image description here

After playing around with it and trying to figure out why it doesn't pick up the style I replicated the html/css in codepen.io.

<main class="container mx-auto mt-28 px-5 flex">
  <div class="w-full">
    <div class="flex justify-between items-center">
      <h1 class="font-bold text-4xl">Tasks</h1>
      <a class="rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" href="/tasks/new">New task</a>
    </div>
    <div class="min-w-full">
      <div class="grid gap-4 grid-cols-3" id="Task_1">
        <div class="my-5">
          <strong class="block font-medium.mb-1">Description:</strong>
          complete page 2 in math exercise book
        </div>
        <div class="my-5">
          <strong class="block font-medium.mb-1">Notes:</strong>
          try also the advanced exercises, but don't spend more than 10 minutes on them
        </div>
        <div class="my-5">
          <strong class="block font-medium.mb-1">Deadline:</strong>
          My Deadline
        </div>
      </div>  
      <div class="grid gap-4 grid-cols-3" id="Task_2">
        <div class="my-5">
          <strong class="block font-medium.mb-1">Description:</strong>
          Description
        </div>
        <div class="my-5">
          <strong class="block font-medium.mb-1">Notes:</strong>
          Some flippin' notes
        </div>
        <div class="my-5">
          <strong class="block font-medium.mb-1">Deadline:</strong>
          My Deadline
        </div>
      </div>  
    </div>
  </div>
</main>

There it rendered perfectly, just as I had intended.

enter image description here

What am I missing? Any help appreciated.

like image 597
quaestor Avatar asked Nov 02 '25 17:11

quaestor


1 Answers

The solution is - as often - quite simple: rtfm!

Shoutout to Akhil, who's blogpost helped me to identify the tailwindcss-rails gem behind the magic of --css tailwind.

With tailwind you have to have a separate watch process running in development, which you can start either as a separate process with rails tailwindcss:watch or by running ./bin/dev which uses foreman to start both the Tailwind watch process and the rails server in development mode.

like image 151
quaestor Avatar answered Nov 05 '25 15:11

quaestor