[英]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.