Reputation: 369
I need a little help with state machine because I'm working with rails 4 I have a initial state called in_analysis and other states called approved and reject and the states approved and reject works fine I don't have problem for pass through to the states, but I can't back to the initial state called in_analysis, here is my code:
app/models/profile.rb
state_machine :state, :initial => :in_analysis do
state :in_analysis, :approved, :rejected
state :approved do
validates :name, presence: true
end
event :approved do
transition in_analysis: :approved
end
event :reject do
transition in_analysis: :rejected
transition approved: :rejected
end
end
and in my controller I have this:
def update
@profile.state = :in_analysis
@profile = current_user.profile
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to edit_profile_path(@profile), notice: t(:update, scope: [:messages, :controllers, :profiles, :successfully]) }
format.json { render :show, status: :ok, location: edit_profile_path(@profile) }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
so my goal is when the profile is updated the state back to the initial "in_analysis" but I don't know why doesn't work because the another state works well.
Thanks for the time ! regards !
Upvotes: 0
Views: 143
Reputation: 2175
Assuming that your state_machine
is correct, the only place that you might be doing wrong is
@profile.state = :in_analysis
@profile = current_user.profile
You assign the state
to :in_analysis
then you actually assign @profile
to old current_user.profile
which is fetched from database thus your assignment to :in_analysis
is discarded.
You can try to swap the two line:
@profile = current_user.profile
@profile.state = :in_analysis
Upvotes: 1