0

I am working on building a blog from scratch as I learn Rails, and I am hitting an issue: in my Post model, I have a form where the poster can add a title, a category tag, and the post entry. These are all added to the table as strings.

Here's where things get tricky. Like any blog post, the entry (the text of the post, in real terms) string will likely be multiple sentences long. For this reason, when I loop the Entry content in the view, it will show the post as a block of text, which I am doing like this:

<% @posts.each do |post| %>
    <div class="row">
            <h2><%= post.title %></h2>
            By <h7><%= post.author %></h7> at <h7><%= post.published_at %></h7>
            <p id="paragraph"><%= post.entry %></p>
    <% end %>

So technically, the content is being shown: it has a title, post info, and the entry, but this long block of text for entry looks pretty bad.

What I'd like to do is be able to split <p><%= post.entry %></p> into multiple paragraphs, say if I made up my own markdown and put # when submitting text to indicate a new paragraph.

I found this old post, which is almost perfect in splitting an array except, it requires you set a finite number of mini-strings (like there should be 3 strings sliced from the original).

Since some of these blog posts may end up having 2, 3, 4, and so on paragraphs, I need a solution that will lead to proper formatting.

I see two possible solutions, and this is my question:

1) Is there a better format for setting up the Post table than having one long string for the post content?

2) If not, what can I do to make the string more manageable from a style perspective when it actually renders in the view?

Possible Questions

1. Why not just use a Gem? This is fair. I wanted to try and make the project more or less from scratch for my own learning, as I am new. That's why I want to experiment with building the Post model and making an intuitive UI for submitting entries.

2. Can I see what the migration file looks like?

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :category
      t.string :title
      t.datetime :published_at
      t.integer :user_id
      t.string :author
      t.string :entry

      t.timestamps null: false
    end
  end
end
Community
  • 1
  • 1
darkginger
  • 652
  • 1
  • 10
  • 38
  • The definition of a paragraph is (roughly) "a few related sentences." The relation is semantic, rather than technical. That said, how do you define a paragraph in terms of your "proper formatting?" This distinction may seem pedantic, but it'll matter to your users, so it should matter to you. – MarsAtomic Jul 14 '16 at 02:32
  • normally you'll use something like markdown for the content and then you'll render it with something like https://github.com/vmg/redcarpet – neydroid Jul 14 '16 at 02:33
  • @MarsAtomic, Yes, good point. When I say paragraph, I literally meant "into multiple

    elements". My reasoning - and obviously there is more sophisticated styling - but this will break the text up when it renders. So if I want three blocks of text, rendering as three

    elements would be a good start. Right now, it can only do it as one.

    – darkginger Jul 14 '16 at 02:35

2 Answers2

2

A simple place to start would be with Rails's simple_format helper.

<%= simple_format post.entry %>

It formats a single line break as a <br /> and 2 consecutive line breaks as a new paragraph.

So this input:

Hi
I'm on a

new paragraph

Would be formatted as such:

<p>
  Hi<br />
  I'm on a
</p>
<p>
  new paragraph
</p>

You could also consider integrating a Markdown parser later if you want to get more advanced.

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
  • Implemented this strategy and combined with other answer to use `text` instead of `string`, and it is working pretty well. Good suggestion! – darkginger Jul 14 '16 at 04:45
1

I highly recommend that instead of a string you use the text as your datatype for :entry

t:text :entry

This is because most database types limit Strings to 255 characters, but Text can be as many as 4,294,967,296 characters.

More info here: Difference between string and text in rails?

Community
  • 1
  • 1
Okomikeruko
  • 1,123
  • 10
  • 22