2016-11-05 4 views
-1

앱 사용자는 항목을 수정하거나 삭제하기 전에 계정을 활성화해야합니다.Rails는 계정이 활성 상태 일 때 상태를 확인합니다.

어떻게 상태를 비활성에서 활성으로 설정할 수 있습니까? 나는 pluginweek에서 state_machine을 사용하여 상태를 설정하고 있습니다.

state_machine initial: :inactive do 
event :activate do 
    state = 'active' 
    end 
end 

정품 인증 조치라고하는 컨트롤러가 전자 메일을 통해 사용자에게 전송됩니다.

def activate 
@entry = Entry.find([:id]) 
if (check_email_link(@entry.exp_date)) 
    if @entry.save 
    flash[:notice] = t("activate") 
    redirect_to @entry 
    else 
     flash[:error] = t("already_activated") 
     redirect_to @entry 
    end 
else 
    flash[:error] = t("timeout") 
    redirect_to @entry.new 
end 

끝 문서 내가 entry.state를 통해 Städte을 설정할 수 있지만 rhis가 작동하지 않습니다 말한다.

왜 항목이 활성화되지 않습니까? 모든 도움을 줄 수 있습니까?

+0

는 ['state_machine']에 대한 설명서 (https://github.com/pluginaweek/를 읽고 상태 머신)? –

+0

예, 문제입니다. 문서는 진술했다. 상태 나는 상태를 설정할 수 있습니다. 하지만이 작동하지 않습니다 – amarradi

+0

작업에 대한 오류 로그가 있습니까? 또는 레일 콘솔에서 메소드를 시도하십시오. '@user.activate'를 실행하고 작동하는지 또는 오류인지 확인하십시오. 나는 [state_machine] (https://github.com/pluginaweek/state_machine)을 사용하고 [# 261] (https://github.com/pluginaweek/state_machine/issues/261) 및 [# 334] (https://github.com/pluginaweek/state_machine/issues/334). 해결책은 대신 [state_machines-activerecord] (https://github.com/state-machines/state_machines-activerecord)를 사용하여 보석을 변경하는 것입니다. – gaga5lala

답변

1

일단 state_machine을 설정하면 코드에 따라 ActiveRecord (abbr. AR) 모델에 몇 가지 방법이 추가됩니다.

예를 들면 다음과 같습니다 (단지 데모 코드, 어쩌면 약간의 오타 |||)

# setup state_machine for model Entry 
class Entry < ActiveRecord::Base 
    state_machine initial: :inactive do 
    event :activate do 
     transition :inactive => :active 
    end 
    end 
end 

다음 state_machine 설정 당신을위한 방법 activate.

경우 state_machine 보석의 샘플 사용의 레일 콘솔

# Create an instance of Entry, you will see the attribute `state` value is "inactive" as your setting. 
@entry = Entry.create 
#=> {:id => 1, :state => "inactive"} 

# Then use the method `activate` state_machine define for you according your setting. You will see `state` been changing to "active". 
@entry.activate 
#=> (sql log...) 
#=> {:id => 1, :state => "active" } 

, 당신은 데이터 모델의 상태를 관리 state_machine 도움말에서 작동, 컨트롤러가 아닙니다.

그래서 코드는이 같은 수 있습니다 :이 수

class SomeController < ApplicationController 
    def some_routes_that_activate_user 
    # (some logic...) 
    @entry.activate 
    end 
end 

희망을 당신 :

+0

귀하의 답변 @ gaga5lala 매우 도움이되었다. 그렇게 사용하기 쉽습니다. 나는 변화시킬 선만 있었다. ' '이벤트 : 활성화 do ** 전환 : 비활성 => : 활성 ** 끝' – amarradi

+0

@amarradi oops, state_machine 사용법의 구문 오류가 있음을 알지 못했습니다. 생각 나게 해 주셔서 고마워요, 제 대답으로 고쳤습니다! – gaga5lala

+0

구문 오류가 아니지만 첫 번째 방법은 저에게 적합하지 않습니다. 왜 내 질문에 -1을 얻었습니까?내가 더 잘할 수 있을까? – amarradi

관련 문제