type mismatch: String given - Trying to match strings in ruby

Arc picture Arc · Dec 25, 2010 · Viewed 7.5k times · Source

I am using authlogic-connect in Rails. I am using a simple haml template where i dont want to show the authorization providers who are already added.

%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"}/

I am encountering the error type mismatch: String. The user.authenticated with returns a string.

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

What is the possible problem?

Stacktrace:

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'

Extracted source (around line #10): -- Shows error on line 10

Answer

tokland picture tokland · Dec 25, 2010

Pass a regular expression as argument to String#=~:

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