Tag: ruby

使用用户或管理模型和Basecamp样式子域设计登录

我有Devise用户和管理员的单独模型。 我也在使用Basecamp风格的子域名。 一切都运行良好,除了一些控制器和操作,我需要能够以用户或管理员身份进行身份validation。 目前我有authenticate_user! 在我的application_controller.rb中设置,我正在使用skip_before_filter跳过那些只有管理员应该有权访问的控制器和操作。 不幸的是,我不能简单地在每个控制器上指定身份validation要求,因为我仍然需要一些控制器和操作才能被用户或管理员访问。 我尝试了一些无济于事的事情。 看来如果我移动authenticate_user! 和authenticate_admin! 在某种子域检测逻辑中,它无法处理。 基本上: current_subdomain = request.subdomains.first if current_subdomain == ‘admin’ authenticate_admin! else authenticate_user! end 我曾经一度能够让它尝试身份validation,但由于某种原因,除了会话控制器需要身份validation导致重定向循环(我的第一个使用Ruby!)之外,它没有成功。 我意识到我可以向我的用户添加一个表示管理员状态的字段,但除了一些控制器和操作之外,应用程序需要在用户和管理员之间实现更大的权限分离。 Ruby 1.9.2 Rails 3.0.3 设计1.1.3

Ruby Sqlite3安装sqlite3_libversion_number()macOS Sierra

我正在尝试安装Metasploit框架(不重要),而bundler正在尝试安装sqlite3,这是它一直失败的地方。 安装了Sqlite3(在命令行执行sqlite3将我带入环境)并使用brew link sqlite3进行brew link sqlite3 (并且由于某种原因添加了–force)但是每次使用此错误时, bundler install都会失败: sudo gem install sqlite3 Building native extensions. This could take a while… ERROR: Error installing sqlite3: ERROR: Failed to build gem native extension. /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb Error: Running Homebrew as root is extremely dangerous and no longer supported. As Homebrew does not drop privileges on installation you […]

Ruby中’self.method_name’和’class << self'之间的区别

我试图将类的实例化限制为只有一个(不使用单例),但我不能。 我尝试使用类变量(@@),但没有运气。 我用Google搜索并发现了这个: class A @count = 0 class << self attr_accessor :count end def initialize val @a = val self.class.count += 1 end end a=A.new 42 b=A.new 43 我搜索了“类”自我 “的解释,希望找到一个更好的(或者只是一个更简单和干净),但是反过来,没有运气。 最后,经过一些测试后我得出结论, ‘class << self ‘只是一个块包装器,你可以在其中定义类方法。 那么,这是正确的吗? 问候!

Ruby SSL错误 – sslv3警告意外消息

我正在尝试使用ruby脚本连接到服务器https://www.xpiron.com/schedule 。 但是,当我尝试连接时: require ‘open-uri’ doc = open(‘https://www.xpiron.com/schedule’) 我收到以下错误消息: OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: sslv3 alert unexpected message from /usr/local/lib/ruby/1.9.1/net/http.rb:678:in `connect’ from /usr/local/lib/ruby/1.9.1/net/http.rb:678:in `block in connect’ from /usr/local/lib/ruby/1.9.1/timeout.rb:44:in `timeout’ from /usr/local/lib/ruby/1.9.1/timeout.rb:87:in `timeout’ from /usr/local/lib/ruby/1.9.1/net/http.rb:678:in `connect’ from /usr/local/lib/ruby/1.9.1/net/http.rb:637:in `do_start’ from /usr/local/lib/ruby/1.9.1/net/http.rb:626:in `start’ from /usr/local/lib/ruby/1.9.1/net/http.rb:1168:in `request’ from /usr/local/lib/ruby/1.9.1/net/http.rb:888:in `get’ from (irb):32 from /usr/local/bin/irb:12:in […]

Ruby变量(数组)赋值误解(使用push方法)

我发现了我对Ruby或编程理论或两者的理解中的一个缺陷。 看看这个代码: #!/usr/bin/ruby -w @instance_ar = [1,2,3,4] local_ar = @instance_ar local_ar_2 = local_ar ### irrelevant_local_ar = [5,6,7,8] ### for i in irrelevant_local_ar local_ar_2.push(i) end count = 0 for i in local_ar_2 puts “local_ar_2 value: #{i} and local_ar value: #{local_ar[count]} and @instance_ar value: #{@instance_ar[count]}\n” count += 1 end 输出是 local_ar_2 value: 1 and local_ar value: 1 […]

在Ruby中,为什么Array.new(size,object)创建一个由对同一对象的多个引用组成的数组?

如本回答所述 , Array.new(size, object)创建一个对同一object具有size引用的数组。 hash = Hash.new a = Array.new(2, hash) a[0][‘cat’] = ‘feline’ a # => [{“cat”=>”feline”},{“cat”=>”feline”}] a[1][‘cat’] = ‘Felix’ a # => [{“cat”=>”Felix”},{“cat”=>”Felix”}] 为什么Ruby会这样做,而不是做object clone或clone ?

我的非模型/非控制器代码应该在哪里生效?

我编写了一个跟随常规目录结构的rails应用程序(模型中的模型代码,控制器中的控制器代码)。 但我现在正在开发一个新function,为此我写了一些(我称之为)“服务”代码。 新function是将一些数据导入系统,目前它有两个类进行导入,但可以扩展到更多。 我不相信新代码属于模型,因为它没有对任何对象建模(它也不直接与任何单个对象相关。我当然不认为它属于控制器,因为它不是表示逻辑。 所以,我创建了一个“app / services”目录并将其放在那里。 我还创建了一个“测试/服务”目录,我已经进行了测试。 我认为一切都很好,但是当我运行’rake:test’或’autotest’时,我的新服务测试没有运行。 现在我希望有一种方法可以让rake拿起它们,但这是一个警告标志,我做错了什么? 代码应该存在其他地方还是我不知道“Rails方式”做什么? 通常,每当我遇到这样的问题之前,我通常都会发现rails已经有了解决方案,但我并不知道这个惯例。 这是其中一个案例吗?

jekyll服务依赖错误 – 无法打开’lib curl’

我试图运行bundle exec jekyll serve但是它出现了错误: Dependency Error: Yikes! It looks like you don’t have jekyll-remote-theme or one of its dependencies installed. In order to use Jekyll as currently configured, you’ll need to install this gem. The full error message from Ruby is: ‘Could not open library ‘libcurl’: The specified module could not be found. . Could […]

每个function之前和之后都有一个黄瓜钩

有没有办法在每个黄瓜function之前和之后运行特定代码块与某些标签? 由于设置过程非常昂贵,我不想在每个场景之前运行它。

量词和后视问题

### Ruby 1.8.7 ### require ‘rubygems’ require ‘oniguruma’ # for look-behind Oniguruma::ORegexp.new(‘h(?=\w*)’) # => /h(?=\w*)/ Oniguruma::ORegexp.new(‘(? ArgumentError: Oniguruma Error: invalid pattern in look-behind Oniguruma::ORegexp.new(‘(? /(? # “hello”.match(/(? SyntaxError: (irb):3: invalid pattern in look-behind: /(?<=\w*)o/ "hello".match(/(? # 我不能使用后视量词吗?