Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Tailwind styled scaffold templates #71

Merged
merged 4 commits into from
Sep 29, 2021

Conversation

BakiVernes
Copy link
Contributor

@BakiVernes BakiVernes commented Sep 3, 2021

Summary

This PR adds Tailwind styled scaffold templates when this gem is installed. These styles were sanctioned by Adam and the Tailwind team. Some examples:

image (4)

Screenshot 2021-09-03 at 09 44 03

Screenshot 2021-09-12 at 19 37 47

TODO

[✓] Make new styles based on Adam Wathan suggestions
[✓] Conform template partials to Rails 7 main branch
[✓] Make it work with Rails 6

Additional information

It would great if we had the option of using Tailwinds @apply directive. This would be useful when extracting components like buttons, inputs etc. But since the css comes precompiled we did what we could.

Maybe something to work on in the future?

Co-authored-by: Dino Marić dino.onex@gmail.com

Co-authored-by: Dino Marić <dino.onex@gmail.com>
@BakiVernes
Copy link
Contributor Author

Resolves #23

@dhh
Copy link
Member

dhh commented Sep 3, 2021

Digging this! Will look closer in a bit. Thanks for working on it!

<%% end %>

<% attributes.each do |attribute| -%>
<div class="field my-5">
Copy link

@hahmed hahmed Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what the field css class is? I don't think that's tailwind related? Maybe it's the scaffold field class here https://github.com/rails/rails/blob/9c77d72ecc7fdf490e778b0807882fd53115e7a3/actiontext/test/dummy/app/assets/stylesheets/scaffold.css#L40? If so, maybe we can drop that because we want to use only tailwind inlined css?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops...fixed

</div>

<% end -%>
<div class="actions inline">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actions also looks like it's from the default scaffold css from https://github.com/rails/rails/blob/9c77d72ecc7fdf490e778b0807882fd53115e7a3/actiontext/test/dummy/app/assets/stylesheets/scaffold.css#L41, maybe drop this too and add mb-something here too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops...fixed. Thank you ❤️

@dhh
Copy link
Member

dhh commented Sep 4, 2021

We have to make this match the new style in rails/main where the _post.html.erb partial is used on the index. That's how it matches up to work out of the box with Turbo. Which means we can't use a table on the index.

See the templates in https://github.com/rails/rails/tree/main/railties/lib/rails/generators/erb/scaffold/templates

@dixpac
Copy link
Contributor

dixpac commented Sep 4, 2021

@dhh no problem.
That was the one of the points we debated internally, main reason why we choose table is that Adam sent us mockups with the table and we didn't wan't to deviate too much from his mockups.

But, yeah I agree that index without the table is going to be easier especially for the scaffolds where the table columns are dynamic and it is hard to style the table properly.

Anyways, we'll match with the Rails 7 changes 👍🏻

<%%= form.password_field :password %>
</div>

<div class="my-5">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think these div's are aligned properly, yeah?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we generate the scaffold the divs are indeed properly aligned as shown in a fresh rails/main app:

image

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have had to add that extra indentation for .tt files that I have worked on in the past too so that the generated files look right as you showed above. That's not the only quirk it has for lining up code. Variables and things also have to be lined up weird so they generate in the correct location. I have never understood why, but would like to look into it more. Any suggestions on where I could dive deeper on this?


<%%= link_to "Show this <%= human_name.downcase %>", @<%= singular_table_name %>, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper %>_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR

<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %>

<%%= link_to 'Back to <%= human_name.pluralize.downcase %>', <%= index_helper %>_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR

<%%= link_to 'Edit this <%= human_name.downcase %>', edit_<%= singular_table_name %>_path(<%= singular_table_name %>), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<%% end %>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR

Add custom scaffold generator that is same as the one inside the Rails 7
in order to make this work with Rails 6.
end
end

template "partial.html.erb", File.join("app/views", controller_file_path, "_#{singular_table_name}.html.erb")
Copy link
Contributor

@dixpac dixpac Sep 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to get the partial in rails 6 I had to "re-implement/copy" Rails 7 scaffold generator :)

@dixpac
Copy link
Contributor

dixpac commented Sep 14, 2021

@dhh I think this should be ready now, and it should also work with Rails 6. Take a 👀 when you have time.

@dhh
Copy link
Member

dhh commented Sep 28, 2021

Finally got a chance to check this out. Looking very good! Only thing I'd say is that we could use some padding. So headings and buttons aren't slammed against the edges of the browser:

Screen Shot 2021-09-28 at 8 30 35 AM

end
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR


%w(index edit new show _form _message).each { |view| assert_file "app/views/messages/#{view}.html.erb" }
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR

@dixpac
Copy link
Contributor

dixpac commented Sep 28, 2021

@dhh re paddings that is strange! Only way I could see that happening is if you don't have a main wrapper(https://github.com/rails/tailwindcss-rails/pull/71/files#diff-82d31adf98ad40bfa67ed05464d36790777703adead66a2478d97320061f136cR9)

Could you push sample app you are showing on the Github so we can double check 🙏

For the reference this is how it looks like on the app I've just created

Screenshot 2021-09-28 at 20 18 39

@dhh
Copy link
Member

dhh commented Sep 29, 2021

I had not run the installer correctly. You're right. It looks great!! This is really neat.

@dhh dhh merged commit b9bf2ae into rails:main Sep 29, 2021
@seanwmitchell
Copy link

@dixpac Thanks for your work on this, I’m finally going to get rid of rails_admin gem (with heaps of dependent gems) and scaffold out the rest of my models. Great work 👍 I appreciate it.

@lafeber
Copy link

lafeber commented Oct 1, 2021

@BakiVernes I absolutely love this, I was working on a similar thing :) Have you considered applying the tailwind classes in css, like daisy-ui does?

@BakiVernes
Copy link
Contributor Author

Thank you @lafeber! We have considered extracting classes, but since this gem comes with a precompiled css file with all of tailwinds utility classes inside (so no preprocessing done) it's gonna be a challenge. But we will definitely take a shot at it :)

@esale
Copy link

esale commented Jun 6, 2022

We have to make this match the new style in rails/main where the _post.html.erb partial is used on the index. That's how it matches up to work out of the box with Turbo. Which means we can't use a table on the index.

See the templates in https://github.com/rails/rails/tree/main/railties/lib/rails/generators/erb/scaffold/templates

I'm sorry if this is not the place for this comment:

@dhh I love Rails, among other things, because it enables me to do very fast functional prototypes. And part of that was having an index scaffold that I could instantly show to a client to display a list of items, and customize later. It was amazing and well known:
Rails automates scaffolding to make early coding more productive than ever before. (Ruby on Rails: Up and Running by Bruce Tate, Curt Hibbs)

Now, with Rails 7.0.3, if I wanted that, do I have to manually code another view? Or maybe there is some CSS that would allow me to show this new index like a table? I'm not UI expert, and with earlier Rails versions, I didn't needed to be.

I'm not the only one troubled with this:

https://www.reddit.com/r/rails/comments/pztx9g/scaffold_index_no_longer_generating_html_tables/
https://www.reddit.com/r/rails/comments/qypvnm/rails_new_scaffold_index/
https://stackoverflow.com/questions/71357307/no-table-view-after-crud-scaffolding
https://stackoverflow.com/questions/72402399/add-new-partial-to-rails-7-generate-scaffold

IMHO this change as it is, may be beneficial to some while detrimental to others. I feel like the small guy being left behind here.

I appreciate very much all the work you have done, @dhh along with all the Rails team.

Regards.

@olivierlacan
Copy link
Contributor

olivierlacan commented Feb 25, 2024

$ rails -v
Rails 7.1.3
rails new --css=tailwind rails-7-1-tailwind
cd rails-7-1-tailwind/
bin/rails g scaffold Post title:string description:text
bin/rails db:migrate
bin/rails s
Screenshot 2024-02-25 at 4 35 23 AM Screenshot 2024-02-25 at 4 36 00 AM Screenshot 2024-02-25 at 4 36 05 AM

@dixpac I'm curious what you did to obtain the output shown in your post. Granted this was likely several versions behind what I used here.

There are no browser console or Rails server errors indicating any issues with loading CSS. So is this is the expected stock output of Rails scaffolds in 7.1.3.2 with tailwindcss-rails 2.3.0?


Update: Never mind, looks like bin/dev is needed here for bin/rails tailwindcss:watch, works fine when booting the server with it:

Screenshot 2024-02-25 at 4 49 22 AM Screenshot 2024-02-25 at 4 49 09 AM Screenshot 2024-02-25 at 4 49 07 AM

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants