У меня есть следующий код.Я все еще новичок в Ruby on Rails.Как видите, я повторяюсь 4 раза.
Я пробовал что-то вроде этого:
if @property.nil? || @property.status_id == 144 || (@property.status_id <= 16 && current_user.nil?) || (@property.status_id <= 16 && current_user.id != @property.user_id)
Но это дает мне много ошибок в случае, если @property равен нулю.Потому что тогда @ property.status_id не может быть вызван, так как @property равен nil.
В любом случае, я думаю, что опытный кодировщик Ruby on Rails понял эту идею.
def show
@property = Property.find(params[:id]) rescue nil
if @property.nil?
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id == 144
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id <= 16 && current_user.nil?
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id <= 16 && current_user.id != @property.user_id
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
@images = Image.find(:all, :conditions =>{:property_id => params[:id]})
end
root