All threads / How to stay strictly in Ruby ?

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.

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

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

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.