【问题标题】:Ruby: parametrize string without Ruby on rails framework (Jekyll plugin)Ruby:在没有 Ruby on rails 框架(Jekyll 插件)的情况下参数化字符串
【发布时间】:2021-07-06 20:00:55
【问题描述】:

我有一个插件,它从帖子的前端获取属性并在永久链接中使用它。问题是我需要在将字符串放入永久链接之前清除字符串中的所有重音符号和变音符号。 Ruby on rails 有一个名为 parametrize 的方法,它完全符合我的需要,但我不知道如何在插件中使用它。

这是我的插件代码:

module JekyllCustomPermalink
  class CustomPermalink < Jekyll::Generator
    safe true
    priority :low

    def generate(site)
      # nothing to do, wait for hook
    end

    Jekyll::Hooks.register :documents, :pre_render do |doc|
      begin
        # check if jekyll can resolve the url template
        doc.url
      rescue NoMethodError => error
        begin
          if !doc.collection.metadata.fetch("custom_permalink_placeholders").is_a?(Array)
            raise CustomPermalinkSetupError, "The custom placeholders need to be an array! Check the settings of your '#{doc.collection.label}' collection."
          end
          def doc.url_template
            @custom_url_template ||= collection.metadata.fetch("custom_permalink_placeholders").inject(collection.url_template){|o,m| o.sub ":" + m, data[m].to_s.parameterize}
          end
        rescue KeyError
          # "custom_permalink_placeholders"
          raise CustomPermalinkSetupError, "No custom placeholders defined for the '#{doc.collection.label}' collection. Define an array of placeholders under the key 'custom_permalink_placeholders'. \nCaused by: " + error.to_s
        end
      end
    end
  end
end

但我收到此错误:

john@arch-thinkpad ~/P/blog (master)> bundle exec jekyll serve --trace
Configuration file: /home/john/Projects/lyricall/_config.yml
            Source: /home/john/Projects/lyricall
       Destination: /home/john/Projects/lyricall/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
       Jekyll Feed: Generating feed for posts
  Liquid Exception: undefined method `parameterize' for "Žďořšťáčik":String in feed.xml
bundler: failed to load command: jekyll (/home/john/.gem/ruby/3.0.0/bin/jekyll)
/usr/lib/ruby/gems/3.0.0/gems/jekyll_custom_permalink-0.0.1/lib/jekyll_custom_permalink/custom_permalink.rb:20:in `block in url_template': undefined method `parameterize' for "Žďořšťáčik":String (NoMethodError)

我做错了什么?我如何使用这个应该是字符串类的一部分但显然不是的方法?在没有 ruby​​ on rails 框架的情况下如何获得相同的结果?

信息:

  • jekyll 4.1.1
  • ruby 3.0.1p64(2021-04-05 修订版 0fb782ee38)[x86_64-linux]

感谢您的帮助

【问题讨论】:

    标签: ruby-on-rails ruby jekyll jekyll-extensions parametrize


    【解决方案1】:

    Rails 对基本 Ruby 类的添加,例如 String#parameterize,是 Active Support Core Extensions 的一部分。 activesupport gem 可以独立于 Rails 安装和使用。

    为了保持较低的默认占用空间,ActiveSupport 允许您 require 仅您想要使用的单个扩展。在您的情况下,您将需要字符串变形扩展:

    require 'active_support/core_ext/string/inflections'
    "Kurt Gödel".parameterize 
    => "kurt-godel"
    

    【讨论】:

      猜你喜欢
      • 2010-10-23
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 2013-04-23
      相关资源
      最近更新 更多