How can i fix this, when i try to condition if user.completed? == false or add when "completed", i will has error
def update_status
status_cont = params[:status_cont]
if user.completed? == false
case status_cont
when "waiting_email_confirm"
user.waiting_email_confirm!
when "email_confirmed"
user.email_confirmed!
when "ready_to_ship"
user.ready_to_ship!
when "shipped"
user.shipped!
when "canceled"
user.canceled!
when "completed"
user.completed!
end
end
end
There's a pattern in your case
that you can use to simplify the logic. Something like this:
STATUS_CONTS = %w[canceled completed email_confirmed ready_to_ship shipped waiting_email_confirm]
private_constant :STATUS_CONTS
def update_status
return if !user.completed?
status_cont = params[:status_cont]
raise "Unknown status_cont #{status_cont}" if !status_cont.in?(STATUS_CONTS)
user.send("#{status_cont}!")
end
You'll probably want to handle unknown status_cont
values differently but I don't know how the surrounding flow works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.