哈希的奇数列表

时间:2011-11-04 18:29:17

标签: ruby-on-rails ruby

我正在尝试从github启动并运行一个rails网站,我遇到了这些错误:

WARNING: 'task :t, arg, :needs => [deps]' is deprecated.  Please use 'task :t, [args] => [deps]' instead.
    at /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.1/lib/sunspot/rails/tasks.rb:41 rake aborted! /Users/robertgrzesik/Documents/RubyOnRails/IndieOption/app/helpers/advertisements_helper.rb:15: odd number list for Hash
          user_id: current_user.id,
                  ^ /Users/robertgrzesik/Documents/RubyOnRails/IndieOption/app/helpers/advertisements_helper.rb:15: syntax error, unexpected ':', expecting '}'
          user_id: current_user.id,
                  ^ /Users/robertgrzesik/Documents/RubyOnRails/IndieOption/app/helpers/advertisements_helper.rb:16: syntax error, unexpected ':', expecting '='
          page: request.url
               ^ /Users/robertgrzesik/Documents/RubyOnRails/IndieOption/app/helpers/advertisements_helper.rb:17: syntax error, unexpected '}', expecting kEND
        })
         ^ /Users/robertgrzesik/Documents/RubyOnRails/IndieOption/app/helpers/advertisements_helper.rb:20: odd number list for Hash
          page: request.url
               ^ /Users/robertgrzesik/Documents/RubyOnRails/IndieOption/app/helpers/advertisements_helper.rb:20: syntax error, unexpected ':', expecting '}'
          page: request.url
               ^ /Users/robertgrzesik/Documents/RubyOnRails/IndieOption/app/helpers/advertisements_helper.rb:21: syntax error, unexpected '}', expecting kEND
        })
         ^

代码:

if ad
  if current_user
    ad.impressions.create({
      user_id: current_user.id,
      page: request.url
    })
  else
    ad.impressions.create({
      page: request.url
    })
  end
  link_to external_redirect_advertisement_url(ad) do
    image_tag ad.image.url(format)
  end
else
  nil
end
end

有什么想法吗?

3 个答案:

答案 0 :(得分:29)

您尝试将1.9哈希语法(key: value)与1.8 Ruby(:key => value)一起使用。

答案 1 :(得分:6)

您运行的是哪个版本的Ruby?乍看之下哈希看起来是正确的,但foo: 'bar'哈希语法仅为1.9+。如果您使用的是Ruby 1.8.x,则需要升级。

答案 2 :(得分:1)

哇,在助手中执行副作用(记录创建)可能是你应该做的最后一件事。无论如何,一个重构提议(我没有看到需要检查current_user,不需要else nil,Ruby默认执行此操作:

if ad
  ad.impressions.create(:user => current_user, :page => request.url)
  link_to external_redirect_advertisement_url(ad) do
    image_tag(ad.image.url(format))
  end
end