所以我在我的应用程序的通讯分发中有这个大方法。方法是更新人造丝,我需要分配一个用户人造丝。我通过表colporteur_in_rayons
关系n:n,其具有属性since_date
和until_date
。帮我重构这个令人讨厌的Ruby if/else语句
我是一名初级程序员,我知道这段代码非常虚拟:) 我很感谢每一个建议。
def update
rayon = Rayon.find(params[:id])
if rayon.update_attributes(params[:rayon])
if params[:user_id] != ""
unless rayon.users.empty?
unless rayon.users.last.id.eql?(params[:user_id])
rayon.colporteur_in_rayons.last.update_attributes(:until_date => Time.now)
Rayon.assign_user(rayon.id,params[:user_id])
flash[:success] = "Rayon #{rayon.name} has been succesuly assigned to #{rayon.actual_user.name}."
return redirect_to rayons_path
end
else
Rayon.assign_user(rayon.id,params[:user_id])
flash[:success] = "Rayon #{rayon.name} has been successfully assigned to #{rayon.actual_user.name}."
return redirect_to rayons_path
end
end
flash[:success] = "Rayon has been successfully updated."
return redirect_to rayons_path
else
flash[:error] = "Rayon has not been updated."
return redirect_to :back
end
end
首先修复格式 - 由4个空格缩进代码 – horseyguy 2010-05-08 11:57:38
这似乎不是一个问题。你可以尝试在refactormycode上发布你的代码。 http://refactormycode.com/ – 2010-05-08 12:06:23
banister,ruby代码通常在两个空格处缩进。 – vise 2010-05-08 12:08:15