我想在内容之间插入图像
I want to insert the image in between the content
我是 rails ruby 的初学者。我正在开发一个可以写文章的博客。
Article table 有 title, 内容 和图像 列。
文章模型
class Article < ActiveRecord::Base
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
文章控制器
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
文章#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 %>
我正在使用"paperclip gem"上传图片。
我可以上传并显示与文章对应的图片。
我需要的是我想在文章内容之间的任何需要的地方显示图像(比如在 Whosebug 中)。请帮我解决这个问题。提前致谢。
Whosebug 的内容使用 Markdown。
您可以像RedCarpet一样使用gem来解析mark down,并呈现类似于Whosebug的内容。
有一个关于此主题的 Rails 投射视频 - Ruby on Rails - Railscasts #272 Markdown With Redcarpet
对于图像,您可以尝试像 CKeditor 这样的富编辑器,与 Paperclip/Carrierwave 挂钩。
通常它会使用一个单独的模型来存储图像。
您将通过 CKeditor 上传图片,它会将图片插入到内容中。
我是 rails ruby 的初学者。我正在开发一个可以写文章的博客。
Article table 有 title, 内容 和图像 列。
文章模型
class Article < ActiveRecord::Base
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
文章控制器
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
文章#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 %>
我正在使用"paperclip gem"上传图片。
我可以上传并显示与文章对应的图片。
我需要的是我想在文章内容之间的任何需要的地方显示图像(比如在 Whosebug 中)。请帮我解决这个问题。提前致谢。
Whosebug 的内容使用 Markdown。
您可以像RedCarpet一样使用gem来解析mark down,并呈现类似于Whosebug的内容。
有一个关于此主题的 Rails 投射视频 - Ruby on Rails - Railscasts #272 Markdown With Redcarpet
对于图像,您可以尝试像 CKeditor 这样的富编辑器,与 Paperclip/Carrierwave 挂钩。 通常它会使用一个单独的模型来存储图像。 您将通过 CKeditor 上传图片,它会将图片插入到内容中。