我安装了一个 gem :http://toopay.github.io/bootstrap-markdown/
这个库应该将 textarea 元素转换为风格化的 mardown 编辑器。
为此,我使用了以下代码:
<div class="well col-md-10 col-md-offset-1">
<%= form_for(:post, :url => {:action => 'create'}) do |f| %>
<%= f.text_field(:title, class: 'form-control')%>
<%= f.text_field(:description, class: 'form-control')%>
<%= f.text_area(:content, rows: 15, "data-provide" => "markdown")%>
<%= f.button "Submit", type: 'submit', class: 'btn col-md-4 col-md-offset-4 btn-large btn-success'%>
<% end %>
</div>
最初,我将此代码放在我的根页面上,它运行得很好。 然后我创建了一个新 Controller 并将其移动到不同的 View 。我通过在浏览器中手动输入 url 来访问该 View :localhost:3000/controller/view 并且页面加载完美。
此处的结果:
但是,当我通过其他页面上的链接访问该页面时,表单并未风格化。
结果:
奇怪的是,如果我重新加载页面,它就会应用所有更改。
我做了一个临时的丑陋黑客,每次访问时都会重新加载第一页:
$(document).ready(function(){
//Check if the current URL contains '#'
if(document.URL.indexOf("#")==-1){
// Set the URL to whatever it was plus "#".
url = document.URL+"#";
location = "#";
//Reload the page
location.reload(true);
}
});
但是对于正在发生的事情必须有一个合理的解释。
我浏览了库本身并添加了一些 console.log 语句。 似乎该库已在主页上加载并初始化,但是当我单击该链接时,它不再运行任何内容。
我非常努力地试图理解这是一个 Rails 问题还是一个库问题。
最佳答案
您在使用 Turbolinks 时遇到问题。首先,删除 data-provide 属性以自行初始化输入并添加一个类,例如 markdown-editor
。
<%= f.text_area(:content, rows: 15, class: "markdown-editor")%>
其次,添加 Javascript 初始值设定项:
$(document).on('ready page:load', function(){
$(".markdown-editor").markdown()
});
然后它将在每次页面加载时初始化。
关于ruby-on-rails - Rails 如何以及何时加载 gem 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908633/