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
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