我想要一个类似于 stackoverflow 上的实时预览。使用 Rails 3.1 和 RedCloth,我似乎无法理解如何使其工作。
我尝试制作自己的 Ajax 调用,例如(在我的 posts.js.coffee 中)
$ ->
$('#post_content').keyup ->
$.post('posts/preview', $('#post_content').val(), null, "script")
并在控制器内部具有功能
def preview
@content = params[:post_content]
respond_to do |f|
f.js
end
end
在 preview.js.erb 我放
$("#preview").html("<% raw RedCloth.new(@content).to_html %>");
我添加了资源
resources :post do
post 'preview', :on => :collection
end
但它不起作用。有什么建议么?