I have the following code. I'm still a newbie in Ruby on Rails. As you can see I'm repeating myself 4 times.
I tried something like this:
if @property.nil? || @property.status_id == 144 || (@property.status_id <= 16 && current_user.nil?) || (@property.status_id <= 16 && current_user.id != @property.user_id)
But it gives me lots of errors in case @property is nil. Because then @property.status_id cannnot be called since @property is nil.
Anyway, I think an experienced Ruby on Rails coder gets the idea.
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