이 중첩 된 if 문을 작동 시키려고하는데 구문이 잘못되어 알아낼 수 없습니다. Rails 4 앱과 정렬 드롭 다운에 검색 창이 있습니다. 검색 결과 페이지에서 정렬 드롭 다운에서 사용자가 선택한 항목을 기반으로 제품 목록을 정렬하고 싶습니다. 사용자가 검색어를 입력하지 않으면 메시지를 표시하려고합니다. 여기 내 컨트롤러 코드가 있습니다.Ruby - 중첩 IF 문 구문
조건을 제거하고 기본 정렬 만 표시하면 검색 결과 페이지가 잘 표시되어 if 문 구문에 오류가 발생합니다.
루비에서def search
if params[:search].present?
@listings =
case (params[:sort])
when "- Price - Low to High"
...
when "- Price - High to Low"
...
when "- New Arrivals"
...
when "- Random Shuffle"
...
else
...
end
else
flash[:notice] = "Please enter one or more search terms e.g. blue shirt."
end
end
하는 case
문의 결과가 할당 할 수 있습니다 :
def search
if params[:search].present?
if params[:sort] == "- Price - Low to High"
@listings = ...
elsif params[:sort] == "- Price - High to Low"
@listings = ...
elsif params[:sort] == "- New Arrivals"
@listings = ...
elsif params[:sort] == "- Random Shuffle"
@listings = ...
else
@listings = ...
end
else
flash[:notice] = "Please enter one or more search terms e.g. blue shirt."
end
end
코드의 정확한 동작은 무엇입니까? 고지가 항상 표시됩니까? – Undo
체인 된'elsif'가 너무 많습니다. 그것은 당신이 "switch"를 많이 사용할 수있는 것처럼 보이는데, 그것은 루비에'case' /'when' 형태로 존재합니다 : http://stackoverflow.com/questions/948135/how-can-i-write-a- switch-statement-in-ruby –
다음을 확인하고 있습니다 : 검색은 있지만 그 다음에 "switching"을 켜십시오 : sort. 이 동작이 정확합니까? –