Reputation: 475
I am beginner in ruby on rails. I am developing a blog where I can write articles.
Article table has title, content and image columns.
Article Model
class Article < ActiveRecord::Base
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
Articles Controller
class ArticlesController < ApplicationController
def index
@articles = Article.all.order("created_at DESC")
end
def new
@article = Article.new
end
def create
@article = Article.new article_params
if @article.save
redirect_to articles_path
else
render 'new'
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update article_params
redirect_to articles_path
else
render 'edit'
end
end
private
def article_params
params.require(:article).permit(:title, :content, :image)
end
end
Articles#new
<%= form_for @article, html: { multipart: true } do |f| %>
<%= f.text_field :title, placeholder: "Title" %><br /><br />
<%= f.text_area :content, placeholder: "Content" %><br /><br />
<%= f.file_field :image %><br /><br />
<%= f.submit %>
<% end %>
I am using "paperclip gem" for uploading images.
I am able to upload and display the image corresponding to the article.
What I need is I want to display the image in between the content of the article whereever I needed(like in stackoverflow). Please help me on this. Thanks in advance.
Upvotes: 1
Views: 774
Reputation: 3984
For images, you can try an rich editor like CKeditor, hooked with Paperclip/Carrierwave. Usually it'll use a separate model to store images. You'll upload images through CKeditor and it'll insert the image in content.
Upvotes: 2
Reputation: 18762
StackOverflow uses Markdown for its content.
You can use gem like RedCarpet to parse mark down, and render content similar to StackOverflow.
There is a Rails Casts video on this topic - Ruby on Rails - Railscasts #272 Markdown With Redcarpet
Upvotes: 3