2017-01-28 2 views
1

내 레일 앱에는 사용자가 프로필을 설정하는 양식이 포함되어 있습니다. 양식의 섹션은 사용자가 긴 목록에서 서비스를 선택할 수 있도록하며 최대 5 개의 다른 서비스를 선택할 수 있습니다. 내장 된 루비 표현 태그 사이의 쉼표

<div class="form-group"> 
 
    <%= f.label :services_1 %> 
 
    <%= f.select :services_1, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %> 
 
    </div> 
 
     
 
     <div class="form-group"> 
 
    <%= f.label :services_2 %> 
 
    <%= f.select :services_2, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %> 
 
    </div> 
 
     
 
     <div class="form-group"> 
 
    <%= f.label :services_3 %> 
 
    <%= f.select :services_3, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %> 
 
    </div> 
 
     
 
     <div class="form-group"> 
 
    <%= f.label :services_4 %> 
 
    <%= f.select :services_4, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %> 
 
    </div> 
 
     
 
     <div class="form-group"> 
 
    <%= f.label :services_5 %> 
 
    <%= f.select :services_5, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %> 
 
    </div>
나는 자신의 프로필에이 목록을하려는 사용자는 단지 2가이

결과, 예를 들어 5 개 미만의 서비스를 선택하지만 경우이

Service 1, Service 2, Service 3, Service 4, Service 5 

같은 페이지

Service 1, Service 2, , , 

여기에 포함 된 내용이 있습니다. 내보기에 파일

<p><%= @user.profile.services_1 %>, <%= @user.profile.services_2 %>, <%= @user.profile.services_3 %>, <%= @user.profile.services_4 %>, <%= @user.profile.services_5 %></p> 

나는 그것이 잉여 쉼표를 제거하기 위해 수정하는 방법?

답변

0

선택한 서비스를 배열로 수집 한 다음 결합하는 것이 좋습니다.

# in the controller 
@services = [] 
@services << @user.profile.services_1 if @user.profile.services_1 
@services << @user.profile.services_2 if @user.profile.services_2 
@services << @user.profile.services_3 if @user.profile.services_3 
@services << @user.profile.services_4 if @user.profile.services_4 
@services << @user.profile.services_5 if @user.profile.services_5 

# in the view 
<p><%= @services.join(', ') %></p> 

이 로그인을 단순화 할 수없는 경우이 로그인을 모델에 배치하는 것이 좋습니다.

# in the model 
def selected_services 
    services = [] 
    services << services_1 if services_1.present? 
    services << services_2 if services_2.present? 
    services << services_3 if services_3.present? 
    services << services_4 if services_4.present? 
    services << services_5 if services_5.present? 
    services 
end 

# in the controller 
@services = @user.profile.selected_services 
+0

감사합니다. HarlemSquirrel! 모델에 배치하라는 두 번째 제안이있었습니다. –

+0

여러분을 환영합니다! 그게 너에게 도움이된다면 내 대답에 투표 할 수있어. – HarlemSquirrel

+0

나는 해냈다. 그러나 내가 15보다 낮은 평판을 가지고 있기 때문에 공적인 점수에 영향을 미치지 않는다는 플래시 메시지가 나온다. –