새로운 사용자를 만든 후에 실행할 메소드를 만들려고합니다. 루비를 사용한 이후로 얼마 동안은 문제가 발생했습니다.Ruby on Rails, 메소드 만들기
이 오류가 발생합니다. C : /Users/antarr/SeniorProject/app/models/user.rb : 9 : 구문 오류, 예기치 않은 tASSOC, expecting keyword_then 또는 ';' 또는 '\ n을'
class User < ActiveRecord::Base
acts_as_authentic
after_create :set_universal
after_create :set_carrier
after_create :set_role
def set_role
if User.count >= 1
endself.roles << "admin"
else
self.roles << "subscriber"
end
end
def set_universal
if Channel.find(1).exist
self.channels << Channel.find(1)
end
def set_carrier
self.carrier = Carrier.with_name(self.carrier_name).first
end
ROLES = %w[admin moderator subscriber]
#Each user can subscribe to many channels
has_and_belongs_to_many :channels
#Each user who is a moderator can moderate many channels
#has_many :channel_mods
has_and_belongs_to_many :modifies , :class_name => "Channel"
#Each user can receive many messages
has_and_belongs_to_many :messages
#Each user belongs to a carrier
belongs_to :carrier
#Filter users by role(s)
named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }
def roles
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
def role_symbols
roles.map do |role|
role.underscore.to_sym # NOT role.name.underscore.to_sym (role is a string)
end
end
end
데프 이 self.roles << "가입자" 끝 끝 –
을 보장 이 self.roles << "관리자" 구조를 시작 set_role 미안 당신이'시작/구조를 필요로 왜 코멘트 –
를 마크 업하는 방법을 모른다 '블록? 내 대답의 코드를 잘 작동합니다. –