Mih*_*kur 1 ruby ruby-on-rails aasm ruby-on-rails-4
我正在使用 AASM 来管理我的用户模型中的状态。
我想通过编辑操作更改状态。
为此,我在表单中使用 User.aasm_states_for_select 方法来填充状态的选择输入。当我点击按钮提交时,所有更改都被保存,包括状态名称。但是那个状态的 AASM 事件没有被调用,它发生是因为只有字段状态发生了变化,而事件方法没有被调用。
有没有人有解决我的问题的方法?
不幸的是,唯一的解决方案是从状态名称中查找事件并直接应用它。就像是
STATE_MAPPING = {
'state_name' => :event_name
}
#...
def update
user.public_send(state_event) if state_event
user.update permitted_attributes
#...
end
# ...
def state_event
state = params.require(:user).permit(:state)[:state]
STATE_MAPPING[state]
end
def permitted_attributes
@params.require(:user).permit #attributes without state
end
Run Code Online (Sandbox Code Playgroud)
太喧嚣了,但据我所知,没有其他可用的解决方案
| 归档时间: |
|
| 查看次数: |
772 次 |
| 最近记录: |