ruby - 类型不匹配 : String given - Trying to match strings in ruby

标签 ruby regex authlogic haml ruby-on-rails-3

我在 Rails 中使用 authlogic-connect。我正在使用一个简单的 haml 模板,我不想在其中显示已添加的授权提供者。

%h2 My Account
%form.authentication_form{:action => connect_path, :method => :post}
  %fieldset
    %input{:type => :hidden, :name => :authentication_type, :value => :user}
    %legend Add another Oauth or OpenID provider.
    .oauth_providers
      %ul
        - %w(google facebook twitter yahoo).each do |name|
          %li.oauth_provider
            -unless "{user.authenticated_with}" =~ "{name}"
              %img{:src => "/images/icons/#{name}.png"}
              %input{:type => :radio, :id => "#{name}_oauth_provider", :name => :oauth_provider, :value => name}
        .clearfix
  %input.submit{:name => :submit, :type => :submit, :value => "Update"}/

我遇到错误类型不匹配:字符串。 user.authenticated with 返回一个字符串。

def authenticated_with
      @authenticated_with ||= self.access_tokens.collect{|t| t.service_name.to_s}
    end

可能的问题是什么?

堆栈跟踪:

ActionView::Template::Error (type mismatch: String given):
    7:       %ul
    8:         - %w(google facebook twitter yahoo).each do |name|
    9:           %li.oauth_provider
    10:             -unless "{user.authenticated_with}" =~ "{name}"
    11:               %img{:src => "/images/icons/#{name}.png"}
    12:               %input{:type => :radio, :id => "#{name}_oauth_provider", :name => :oauth_provider, :value => name}
    13:         .clearfix
  app/views/users/edit.html.haml:10:in `=~'
  app/views/users/edit.html.haml:10:in `_app_views_users_edit_html_haml___2062853011_2171399360_0'
  app/views/users/edit.html.haml:8:in `each'
  app/views/users/edit.html.haml:8:in `_app_views_users_edit_html_haml___2062853011_2171399360_0'

提取的源代码(大约第 10 行): -- 在第 10 行显示错误

最佳答案

将正则表达式作为参数传递给 String#=~ :

>> string = "el"
>> "hello" =~ /#{string}/
=> 1

关于ruby - 类型不匹配 : String given - Trying to match strings in ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4531059/

相关文章:

css - 使用来自 Compass/Codekit 的不同配置将一个 SASS 编译为两个 css

ruby-on-rails - 部署 Rails 应用程序

ruby-on-rails - 使用 AuthLogic 保护内容

ruby-on-rails - 使用 authlogic_api 访问 Rails REST API

ruby - 数据映射器 : one-to-many relationship with custom name?

ruby-on-rails - 如何返回基于一天的 future 日期列表(例如,星期一到年底的日期)?

regex - 变量是变量 bash 的子字符串

javascript - 不以序列开头和结尾的字符串的正则表达式

JavaScript RegExp 排除特定文件扩展名和index.js

testing - rspec 没有收到保存方法