-
-
Save aya-soft/5c62ef8f26c7a911507f99748971fc09 to your computer and use it in GitHub Desktop.
def new | |
unless current_user.service_station.present? | |
redirect_to new_profile_service_station_path | |
else | |
service = current_user.service_station.service_station_comments.where("spare_id = ?", params[:spare_id]) | |
if service.present? | |
redirect_to "/profile/service_station_comments/#{service.first.id}/edit" | |
end | |
@comment = ServiceStationComment.new | |
gon.labor_times_price = current_user.service_station.labor_times_price | |
end | |
end |
Еще по этому:
service = current_user.service_station.service_station_comments.where("spare_id = ?", params[:spare_id])
if service.present?
redirect_to "/profile/service_station_comments/#{service.first.id}/edit"
end
Ты же знаешь, что для одной запчасти может быть только один комментарий от одного сервиса!
Тогда зачем сначала выбирать по where, а потом делать first???
Можно же сразу так:
@existing_comment = current_user.service_station.service_station_comments.find_by(spare_id: params[:spare_id])
Еще по этому:
service = current_user.service_station.service_station_comments.where("spare_id = ?", params[:spare_id])
if service.present?
redirect_to "/profile/service_station_comments/#{service.first.id}/edit"
end
-
Как уже говорил вместо if service.present? достаточно просто if service
-
Ты забыл как url-хелперы делать? Огород городишь :(
# ВМЕСТО
if service.present?
redirect_to "/profile/service_station_comments/#{service.first.id}/edit"
end
# НАДО
redirect_to edit_profile_service_station_comments(@existing_comment) if @existing_comment and return
def new
if current_user.service_station
@existing_comment = current_user.service_station.service_station_comments.find_by_spare_id(params[:spare_id])
if @existing_comment
redirect_to edit_profile_service_station_comment_path(@existing_comment.id)
end
@comment = ServiceStationComment.new
@labor_times_price = current_user.service_station.labor_times_price
else
redirect_to new_profile_service_station_path, notice: "Сначала добавьте автосервис!"
end
end
Когда выполняется and return продолжается выполнение кода в текущем контроллере, что нам не нужно, т.к. у нас переменная comment будет nil, потому что не может быть комментариев у несуществующего автосервиса. Финал привожу
def new
if current_user.service_station
@existing_comment = current_user.service_station.service_station_comments.find_by_spare_id(params[:spare_id])
redirect_to edit_profile_service_station_comment_path(@existing_comment) if @existing_comment
@comment = ServiceStationComment.new
@labor_times_price = current_user.service_station.labor_times_price
else
redirect_to new_profile_service_station_path, notice: "Сначала добавьте автосервис!"
end
end
Почему ты назвал переменную service, когда ты достаешь из базы коментарИИ, т.е. и имя неправильное и множественное число должно быть!