开发者

Problem using :default => {:format => 'pdf'} in Rails 3

开发者 https://www.devze.com 2023-02-12 03:40 出处:网络
I want to route requests something like this: reports/bloodtypes is routed to controller reports, action bloodtypes, with format = pdf, and the route named as bloodtype_report. The Guides gives an exa

I want to route requests something like this: reports/bloodtypes is routed to controller reports, action bloodtypes, with format = pdf, and the route named as bloodtype_report. The Guides gives an example

match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }

When I do this:

match 'reports/bloodtypes' => 'reports#bloodtypes', :defaults => {:format => 'pdf'}, :as => 'bloodtype_report'

or this

match 'reports/bloodtypes' => 'reports#bloodtypes', :format => 'pdf', :as => 'bloodtype_report'

the controller still does not receive the :format => 'pdf' in params, and tries to render the report as HTML. The funny thing is that the route is shown by Rake as

 bloodtype_report :  /reports/bloodtypes(.:format) : {:format=>"pdf", :controller=>"reports", :action=>"bloodtypes"}

whether I use the firs开发者_开发技巧t form (with :default) or second (just setting the format to pdf). It seems the route is correct, so why is the format parameter not being passed to the controller?


have you tried adding this to your controller:

respond_to do |format|
  format.html
  format.pdf { render :pdf => "show" }
end
0

精彩评论

暂无评论...
验证码 换一张
取 消