ruby on rails - Calling a specific respond_to block method -
i trying format link_to tag in order generate pdf file (wicked_pdf gem) following respond_to block:
respond_to |format| format.html format.pdf render pdf: "file_name" end end i have created blocks javascript method. easy js rendered : add remote: true form.
in case of other mime types or respond_to block methods, have no clue how write link_to tag let controller know want specific method against another...
one of ways respond_to block determines format render looking @ extension of url. instance, if have in routes:
rails.application.routes.draw 'home/index', as: 'home' end and in controller:
class homecontroller < applicationcontroller def index respond_to |format| format.html { render inline: 'html' } format.pdf { render inline: 'pdf' } end end end i can format.pdf using .pdf extension:
$ curl http://localhost:3000/home/index.pdf # pdf $ curl http://localhost:3000/home/index.html # html when creating link_to, can tell extension use passing url_for (or whichever named route helper you're using):
<%= link_to 'view pdf', url_for(controller: 'home', action: 'index', format: 'pdf') %> # => <a href="/home/index.pdf">view pdf</a> <%= link_to 'view pdf', home_path(format: 'pdf') %> # => <a href="/home/index.pdf">view pdf</a> the way believe remote: true works setting accept header. rails uses figure out respond_to item call:
$ curl -h 'accept: application/pdf' http://localhost:3000/home/index # pdf
Comments
Post a Comment