在可以编辑或删除条目之前,应用程序的用户必须激活该帐户。
如何将状态从非活动状态设置为活动状态? 我正在使用pluginaweek中的state_machine来设置状态。
state_machine initial: :inactive do
event :activate do
state = 'active'
end
end
我的控制器名为activate-action,将通过电子邮件发送给用户。
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但是这不会起作用。
为什么输入没有被激活?能帮到我吗?
答案 0 :(得分:1)
设置state_machine后,它会根据您的代码向ActiveRecord(简称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
。
如果您在rails控制台中操作
# 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 gem的示例用法,state_machine帮助您管理数据模型的状态,而不是控制器。
所以,你的代码可能是这样的:
class SomeController < ApplicationController
def some_routes_that_activate_user
# (some logic...)
@entry.activate
end
end
希望你能这样做:)