2016-11-09 1 views
0

내 응용 프로그램의 여러 개체에 대한 인덱스보기에 내보내기 기능을 추가하고 싶습니다. 인덱스 뷰는 모두 동일하게 보이므로 부분 뷰를 사용합니다.레일스 5에서 부분적으로 URL을 매개 변수로 전달하는 방법은 무엇입니까?

Download: 
    <%= link_to "CSV", business_objects_path(format: "csv") %> | 
    <%= link_to "Excel", business_objects_path(format: "xls") %> 

:

<% provide(:title, (t('ManagingBOs'))) %> 
<%= will_paginate %> 
<%= render partial: "shared/simple_export", locals: {this_path: business_objects_path} %> 
<%= render partial: "shared/object_index", locals: {this_index: @business_objects} %> 
<%= will_paginate %> 
<br /> 

나는이 부분에서 잘 작동 이와 같은 수출 링크를 정의하는 부분 simple_export을 싶습니다 여기 는 비즈니스 오브젝트에 대한 index.html.erb입니다 아쉽게도 <%= link_to "CSV", this_path(format: "csv") %> 구문은 으로 정의되지 않은 메서드 오류가 발생합니다.

이 부분을 어떻게 구현할 수 있습니까?

+0

표시되는 정확한/전체 오류 메시지를 추가하십시오. – jeffdill2

+0

또한 왜 부분적으로'business_objects_path'를 사용하는 대신에'this_path'를 통해'business_objects_path'를 부분적으로 전달하기를 원합니까? – jeffdill2

답변

0
<%= link_to "CSV", url_for(:format => 'csv') %> | 
<%= link_to "Excel", url_for(:format => 'xls') %> 

경고 :이 버전은

<%= link_to "CSV", params.merge(:format => 'csv') %> | 
<%= link_to "Excel", params.merge(:format => 'xls') %> 

가 필요없이, 귀하의 의견에 어디서나 잘 작동합니다도 작동,하지만 잠재적으로 위험 할 수 여분의 변수를 사용하십시오.

+0

필터링되지 않은 매개 변수를 에코 백하는 것은 클릭 재킹 또는 기타 사악한 동작의 메커니즘으로 사용할 수 있습니다. – max

+0

주석 주셔서 감사합니다. 첫 번째 옵션은 괜찮습니까? –

+1

그래, 보통 문제는 아니야. 그 사실을 알고 있어야합니다. – max

1

당신이 할 때 : 당신은 방법 business_objects_path 전달되지

locals: {this_path: business_objects_path} 

. 메소드 business_objects_path을 호출하고 결과 문자열을 부분 문자열에 전달합니다. Ruby의 메소드 호출에서는 괄호를 생략 할 수 있습니다.

루비의 메소드 (함수 참조)는 method 방법을 사용을 전달하려는 경우

locals: {this_path: self.method(:business_objects_path) } 

방금 ​​대신 방법을 조회하는 다형성 경로 도우미를 사용할 수 있습니다 그러나.

<%= render partial: "shared/simple_export", resource_name: :business_objects %> 

Download: 
    <%= link_to "CSV", polymorphic_path(resource_name, format: "csv") %> | 
    <%= link_to "Excel", polymorphic_path(resource_name, format: "xls") %> 
+0

당신은 오자가 있습니다 : polymorpic -> polymorphic. –

+0

고정. 감사합니다 @ EricDuminil – max

+0

매우 흥미롭고 자세한 설명에 감사드립니다. – user1185081

관련 문제