Middleman blog - How to ensure that article.path translates to an absolute path?

夙愿已清 提交于 2020-01-24 15:02:50

问题


I hope somebody can help -

In my Middleman project, I want the blog articles to be in a subdirectory inside /source/ (so for instance '/source/webdev/blog/). I've followed the instructions, and muddled through (I'm new to Middleman and a complete Ruby newbie) and got the links to appear when calling: article.path HOWEVER - the links appear as relative urls, regardless of what I set in config.rb.

I've disabled :relative_assets (didn't seem to make any difference anyway), and I'm not using 'directory_indexes'. Any help would be immense!

My config.rb looks like this:

activate :blog do |blog|
  # set options on blog
  blog.permalink = "/webdev/blog/{year}/{title}.html"    # but article.path ends up relative!
  blog.sources = "/webdev/blog/{year}/{title}.html"   
end

So my blog articles get built to /webdev/blog/2014/test-article.html, which is what I want.

In a partial that I'm hoping to use anywhere on the site, not just the homepage:

<ul>
     <% blog.articles.each do |article| %>
       <li>
         <%= link_to article.title, article.path %>
       </li>
     <% end %>
</ul>

This gives me a list of links, as expected, but with relative URLs - which means, yep, when the links list is on a page in /webdev/, they take me to /webdev/webdev/blog/2014/some-article.html grrrr...

What could I be doing wrong? Is there some setting in options I've missed?

Thanks in anticipation


回答1:


Well, this isn't quite the answer I was looking for, but I've managed to 'fix' the issue for now - not very elegantly, I'll admit (Did I mention I don't speak Ruby?).

After some desperate messing about, I physically added a forward slash to article.path like so:

= link_to article.title, '/'+ article.path

..and it worked.

I'm going with this for now.

I would still love to know if there is a real solution to this little problem out there...




回答2:


The following configuration needs to be added to your config.rb:

activate :blog do |blog|
 blog.prefix = "webdev/blog"
end

Your blog posts should then be generated in your desired location.



来源:https://stackoverflow.com/questions/27461067/middleman-blog-how-to-ensure-that-article-path-translates-to-an-absolute-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!