Mihir Kumar Thakur
Mihir Kumar Thakur

Reputation: 393

change AASM state through edit action

I'm using AASM to manage states in my User model.

I want to change the status through the edit action.

To do this I'm using the method User.aasm_states_for_select in my form to populate the select input for states. When I hit the button commit all the changes are saved, including the state name. But the AASM event for that state is not called, it is happening because only the field status has changed and the event method has not been called.

Does anyone have a solution for my problem?

Upvotes: 1

Views: 1775

Answers (1)

Nikita Misharin
Nikita Misharin

Reputation: 2030

Unfortunately, the only solution there is to find event from state name and apply it directly. Something like

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

Too much hustle tbh, but to my knowledge there is no other solution available

Upvotes: 5

Related Questions