2017-12-18 11 views
1

스프레드 시트로 내보낼 수있는 옵션이있는 양식이 있지만 내 페이지의 보고서와 동일한 현재 매개 변수 (필터)가 있어야 스프레드 시트가 필요합니다. 이 같은 뭔가 :이처럼 그렇게 관리했습니다현재 모든 매개 변수를 경로로 보내는 방법은 무엇입니까?

<a href="<%= reports_orders_path(params, format: 'xlsx') %>"> 
    <span><i class="fa fa-file-excel-o"></i></span> 
    <%= t '.export_xlsx' %> 
    </a> 

:

<a href="<%= reports_orders_path(
    "by_event" => @event.id.to_s, 
    "by_document" => params[:by_document], 
    "by_status" => params[:by_status], 
    "by_method" => params[:by_method], 
    "by_date" => params[:by_date], 
    "by_period_init" => params[:by_period_init], 
    "by_period_end" => params[:by_period_end], 
    format: 'xlsx') %>"> 
    <span ><i class="fa fa-file-excel-o"></i></span> 
    <%= t '.export_xlsx' %> 
    </a> 

하지만이 느낌이 너무 더러워 보인다.

현재 매개 변수를 모두 가져 와서 내 경로에 적용 할 수있는 더 좋은 방법이 있습니까? 마크 업을 정리하는

+0

당신이 * 모든 * 귀하의 방법으로 키워드 인수로 PARAMS의 물건을하려는 경우, 당신은 by_event (report_orders_path'로 할 수있는 : event.id.to_s @, 형식 : 'XLSX ', ** params)'. Ruby 2 이상이 필요합니다. – user1934428

답변

1
# x_controller.rb 
def action 
    [...] #your code 
    @filters = report_filters 
end 

def report_filters 
    extract_fields = params.keys - ["_method", "authenticity_token", "commit", "controller", "action"] 

    { format: :xlsx, by_event: @event.id }.merge(params.slice(*extract_fields)) 
end 

예를 들어?

나는 시간이 지날수록 유지 보수가 불가능하고 내 경험으로 인한 엄청난 기술적 빚을 짊어지기 쉽기 때문에 헬퍼가 절대적으로 싫다.

편집 : 동적 PARAMS 추출

+0

일했습니다! 고맙습니다 –

0

한 가지 방법과 같이, 뷰 도우미를 원하는 PARAMS을 추출 할 수 있습니다 :

:

# app/helpers/application_helper.rb 

def filter_params 
    { 
    by_event: @event.id.to_s, 
    by_document: params[:by_document], 
    by_status: params[:by_status], 
    by_method: params[:by_method], 
    by_date: params[:by_date], 
    by_period_init: params[:by_period_init], 
    by_period_end: params[:by_period_end], 
    format: 'xlsx' 
    } 
end 

그런 다음보기에서 PARAMS을 채우는 도우미 메서드를 호출

# app/views/your/view/path.html.erb 

<%= link_to reports_orders_path(filter_params) do %> 
    <span><i class="fa fa-file-excel-o"></i></span> 
    <%= t '.export_xlsx' %> 
<% end %> 

희망이 도움이됩니다.

+0

그건 좋은 방법입니다! 그것은 정적이지만, 내 애플 리케이션 이후 더 많은 필터가 필요할 수 있습니다 내 이상적인 솔루션이 아닙니다. –

+0

아, 아마도'request'의 [query_parameters] (http://guides.rubyonrails.org/action_controller_overview.html#path-parameters-query-parameters-and-request-parameters)가 유용 할 수 있습니다. – Zoran