[英]Redcarpet gem with Angularjs in rails application
我正在使用Redcarpet gem渲染降价促销。
这是我的posts_helper.rb
文件:
module PostsHelper
def markdown
options = [:autolink => true, :space_after_headers => true, :hard_wrap => true, :prettify => true]
Redcarpet::Markdown.new(Redcarpet::Render::HTML, *options)
end
end
它可以在我的视图文件index.html.erb
。
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= markdown.render(post.summary).html_safe %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
但是,在我使用Angular.js之后,它不起作用。 Redcarpet无法解析angular.js中的数据。 我应该如何解决这个问题。
# app/assets/javascripts/app.js.coffee
window.App = angular.module('AngularPosts', ['ngResource'])
# app/assets/javascripts/angular/controller/posts_ctrl.js.coffee
App.controller 'PostsCtrl', ['$scope', 'Post', ($scope, Post) ->
$scope.posts = Post.query()
]
# app/assets/javascripts/angular/services/posts.js.coffee
App.factory 'Post', ['$resource', ($resource) ->
$resource '/posts/:id', id: '@id'
]
# app/views/home/index.html.erb
<div ng-controller="PostsCtrl">
<ul>
<li ng-repeat="post in posts">
<h3><%= '{{post.title}}' %></h3>
<div><%= markdown.render("{{post.summary}}").html_safe %></div>
</li>
</ul>
</div>
如何正确呈现数据?
我得到的是这样的:
在浏览器中:
您需要发送预渲染的post.summary
作为JSON有效负载的一部分。 或者,您可以使用JavaScript替代方法,例如Showdown.js在客户端渲染markdown。
渲染视图时,这些行仅渲染一次。
<h3><%= '{{post.title}}' %></h3>
<div><%= markdown.render("{{post.summary}}").html_safe %></div>
给你:
<h3>{{post.title}}</h3>
<div><p>{{post.summary}}</p></div>
在这一点上,Angular接管了。 目前没有其他Ruby。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.