1

我正在使用 AASM 来管理我的用户模型中的状态。

我想通过编辑操作更改状态。

为此,我在表单中使用 User.aasm_states_for_select 方法来填充状态的选择输入。当我点击提交按钮时,所有更改都会保存,包括状态名称。但是该状态的 AASM 事件没有被调用,它正在发生,因为只有字段状态发生了变化并且没有调用事件方法。

有没有人可以解决我的问题?

4

1 回答 1

5

不幸的是,唯一的解决方案是从状态名称中查找事件并直接应用它。就像是

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

太忙了,但据我所知,没有其他解决方案可用

于 2017-11-08T10:37:19.907 回答