在保存到数据库之前,我正在尝试在:body中进行标记。这是我的创建方法
def create
@post=current_user.posts.create(:title => params[:title],:body => markdown(params[:body])
redirect_to(post_path(@post))
end这是我的渲染标记方法
def markdown(text)
options = {
filter_html: true,
hard_wrap: true,
link_attributes: { rel: 'nofollow', target: "_blank" },
space_after_headers: true,
fenced_code_blocks: true
}
extensions = {
autolink: true,
superscript: true,
disable_indented_code_blocks: true
}
renderer = Redcarpet::Render::HTML.new(options)
markdown = Redcarpet::Markdown.new(renderer, extensions)
markdown.render(text).html_safe
end但是,当我输入数据并提交空对象时,就会被归纳。
irb(main):001:0> Post.last
Post Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY
"posts"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Post id: 7, title: nil, body: nil, created_at: "2017-04-26
11:23:12", updated_at: "2017-04-26 11:23:12", user_id: 1>我的表格:-
= simple_form_for @post do |f|
=f.input :title
=f.input :body
=f.button :submit发布于 2017-04-26 12:20:16
应该是这样的:
@post=current_user.posts.create(:title => params[:post][:title],:body => markdown(params[:post][:body]) 此外,您可能希望检查Rails StrongParam是否增强了安全性。
https://stackoverflow.com/questions/43633350
复制相似问题