Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to stay strictly in Ruby ?

Tommy asked in Rails

Hello, I'm a newbie at Rails. Let's say I have this simple form:

<%= form_for @article do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description %>
<%= f.text_area :description %>
<%= f.submit %>
<% end %>

This form works fine. But, what I would prefer to do is this:

<%=
form_for @article do |f|
f.label :title
f.text_field :title
f.label :description
f.text_area :description
f.submit
end
%>

See how clean the code is! Consider if this were a large form, carrying along the open and close Ruby tags (<%=) and (%>) EVERY single line would be a pain in the neck and inefficient! Now, let's say if I need to include any HTML tag (such as p or br) to render the form the way I want, is there a way to include any HTML tag OR text without leaving Ruby ? I know I can easily achieve this if I were to code in PHP. It would be a distraction to switch back and forth between Ruby and HTML, especially when working with a large form.

Reply

If less typing is what you solely after, you might be interested in Slim or Haml.

Reply

eelcoj, I appreciate your response! It's not about less typing, it's about keeping the code clean and compact. Thanks!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 77,200+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.