新手问题 Rails 中想在 model 中添加两个变量,去存储 controller 中传过来的变量,并且这两个变量在整个 model class 中都可用,应该怎么做

qq2729877005 · August 30, 2017 · Last by yqwoe replied at August 30, 2017 · 1346 hits

ails 中想在 model 中添加两个变量,比如 a,b
,去存储 controller 中传过来的变量,比如 c,d
并且这两个变量 (a,b) 在整个 model class 中都可用,应该怎么做?

可以存一个类变量在 model 中,既然是从 controller 中传过来的值,就要注意这个值的初始化时间

attr_accessor :key, :token

那怎么用起来呢? 比如我要给 key 这个变量 4. 给 token 5

class Test
  attr_accessor :key,:token

  def hi
    @key  
  end

end

test =Test.new
test.key = 4
test.hi()    => 4

qq2729877005 closed this topic. 30 Aug 16:28
You need to Sign in before reply, if you don't have an account, please Sign up first.