google1254
google1254

Reputation: 157

How to convert a text input form to html in rails?

I'm trying to create a post model that has a text associated with it as the post. However, as I want to use it as a blog, I want to convert the text into html, like if my input text has blah I want it to display a large header.

This is my input form for the Post model so far:

 <p>
    <%= f.label :Title %><br>
    <%= f.text_field :Title %>
  </p>

  <p>
    <%= f.label :Post %><br>
    <%= f.text_area :Post %>
  </p>

So when I write a post, and use hello it shows up as that instead of html.

And this is my Post model

class Post < ActiveRecord::Base
  attr_accessible :Post, :Title
  has_many :comments, dependent: :destroy
  validates :title, presence: true, length: { minimum: 5 }
end

Upvotes: 2

Views: 1169

Answers (2)

prash
prash

Reputation: 121

Are you able to save posts into database? If yes then write below code

index method in your post controller

def index
posts = Post.all
end

index.html.erb

<%= @posts.each do |f| %>
  <%= f.Title %>
  <%= f.Post %>
<%end%>

Hope I answered what you were looking for.

Upvotes: 0

Amar
Amar

Reputation: 6942

You can use ckeditor,wysihtml5 whatever you prefered,so on text box you can have description with HTML Editor.

Now when you want to show HTML in UI

<%= @post.post.html_safe %> # it will convert your post field into an HTML

Upvotes: 2

Related Questions