Home change AASM state through edit action
Reply: 1

change AASM state through edit action

Mihir Kumar Thakur
1#
Mihir Kumar Thakur Published in 2017-11-08 10:07:19Z

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?

Nikita Misharin
2#
Nikita Misharin Reply to 2017-11-08 10:37:19Z

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_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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.336325 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO