【问题标题】:Rails 3: Class method authenticationRails 3:类方法认证
【发布时间】:2012-08-21 03:17:07
【问题描述】:

我有一个简单的授权功能,但我很难正确实施。每个帖子在创建时都会发出一个 :key_code ,并且用户(未会话)可以键入代码并删除他们的帖子。问题是,无论他们为 key_code 键入什么,对象都会被传递。

为什么总是返回 0?

    field :key_code, type: Integer

    def self.auth(id, key_code)
        post = Post.where(:id => id).first

        puts key_code        #for testing:
        puts post.key_code   #for testing: 

        if post.key_code == key_code
          return 1
        else
          return 0
        end

end

控制台测试:

1.9.3p194 :001 > a = Post.auth('5032f3254ff9fcf10100001b','42745590875')
42745590875
42745590875
=> 0 

【问题讨论】:

  • 您是使用post_id 还是简单地使用id 作为posts 表中的主键?
  • 是的。它不是嵌套资源,只是 mongoid

标签: ruby-on-rails ruby-on-rails-3 class mongoid


【解决方案1】:

看起来您使用的是赋值运算符 (=) 而不是相等 (==)。将该行更改为:

if post.key_code == key_code

【讨论】:

    【解决方案2】:

    我明白了。它还将第二个数字作为字符串传递!呵呵

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多