Rails Newb here asking his first question….
I’m building a personal blog-style website that uses a generic “Post” scaffold to add content to the site. The fields are title:string, body:text, and :tag_list respectively. Acts-as-taggable sorts the posts and puts them on the correct page.
When I create a new post and enter text into the :body, I can’t get it to recognize anything other than pure text. I’m trying to get it to accept in-line html tags, rails methods (i.e. link_to, image_tag) in the text itself. As I’m typing this I realize what I’m looking for is the basic functionality of most blogging text editors. Is there any easy way to do this?
Here is my posts model:
class Post < ActiveRecord::Base
has_many :tags
acts_as_taggable_on :tags
attr_accessible :tag_list, :tags, :title, :body, :post, :comments
validates :title, :presence => true, :length => { :maximum => 30 }
validates :body, :presence => true
default_scope :order => 'posts.created_at DESC'
named_scope :by_join_date, :order => "created_at DESC"
Here is my posts helper:
module PostsHelper
include ActsAsTaggableOn::TagsHelper
include TagsHelper
The Posts Controller and Post Views are all basicallu unchanged from the scaffold code.
Thanks in advance for all your help!
~Dan
(edited for Rails 3 as per the comment below)
If I understand the question, you want the user to be able to enter HTML in their post content.
First off if you’re entering HTML tags manually but they show up as plain text (e.g. <img src=”…”/> instead of the actual image) you have to go into your view and add “raw” before the post body.
should be
Rails 3 by default protects you against users that try to insert HTML into unwanted parts of your site. For example without it if I leave a comment on your post, I could insert some malicious <javascript> tags. But if you’re the only one making posts, that’s obviously not an issue, so it’s safe to display the “raw” html content.
As for the HTML itself, you could conceivably make your text field interpret rails code (like the link_to or image_tag helpers) but that’s bat country, unless you’re really, really sure what you’re doing I wouldn’t go there. (And even if you do know I wouldn’t recommend it.)
You can look into a simpler markup language like RedCloth, very easy to implement but also somewhat limited, or you can go all-out WYSIWYG like CKEditor or WYMEditor
StackOverflow uses WMD-editor, a combination of a simple markup engine and a WYSIWYG text field.
I usually go with CKE because it has a very complete feature set and it’s easy to use for people who are used to MSWord-like editors.