[RUBY] [Rails] Specify format with link_to

As the title says, it ’s about how to specify the format with the link_to method.

It's pretty easy, but I'll leave it as a reminder because the article didn't hit unexpectedly.

What is format in the first place?

The output format The html file is rendered in html format, right? There are other json format, pdf, csv, etc.

This time I will try assuming PDF output

How to write link

Just specify format as the argument of path!

slim file


= link_to 'Display name', xxx_path(format: :pdf)

If you want to specify another id or set a parameter, you can just go into the argument in the same way.

slim file


= link_to 'Display name', xxx_path(format: :pdf, id: @post, parameter_name: parameter_content)

Processing in the controller

In the controller, it is OK if you specify the format and describe the process In the case of pdf, I think that send_file is often used to display or download the file. If you write as follows, the PDF file will be opened on the browser by specifying the file name.

controller


class HogesController < ApplicationController
  def show
    respond_to do |format|
      format.pdf do
        send_file(pdf_path, filename: filename, disposition: 'inline')
      end
    end
  end

  private

  def pdf_path
    #Specify the save location of pdf
    @post.pdf.path #← If you use carrierwave, it looks like this
  end

  def filename
    #Specify the pdf file name
    "#{@post.id}.pdf"
  end
end

Digression

When you do rails routes, you can see (.: Format) in the URI pattern. This will be the URI when format is specified 23d955ea526f9852065207a191d12617.png

For example, if the format is pdf like this time, the URL will be something like https://xxxxxx/xxx.pdf. It means that it will be added in the form of .format format at the end depending on the format ~

Recommended Posts

[Rails] Specify format with link_to
Prepare the format environment with "Rails" (VScode)
[Rails 6] method :: delete cannot be used with link_to
Rails deploy with Docker
[Rails 6] RuntimeError with $ rails s
Handle devise with Rails
[Rails] Test with RSpec
Automate format with maven-formatter
[Rails] Development with MySQL
Format JSON with org.json
Supports multilingualization with Rails!
Double polymorphic with Rails
Created RSS / Atom format sitemap with Ruby on Rails
Introduced graph function with rails
[Rails] Express polymorphic with graphql-ruby
[Rails] Upload videos with Rails (ActiveStorage)
Try using view_component with rails
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
Japaneseize using i18n with Rails
Format Ruby with VS Code
API creation with Rails + GraphQL
Preparation for developing with Rails
Run Rails whenever with docker
How to specify db when creating an app with rails
[Docker] Rails 5.2 environment construction with docker
Use multiple databases with Rails 6.0
Login function implementation with rails
Pass parameters to Rails link_to
[Docker] Use whenever with Docker + Rails
[Rails] I want to add data to Params when transitioning with link_to
Rails + MySQL environment construction with Docker
Create portfolio with rails + postgres sql
[Rails] Push transmission with LINE Bot
[Rails] Make pagination compatible with Ajax
Implemented mail sending function with rails
Minimal Rails with reduced file generation
Create pagination function with Rails Kaminari
Build environment with vue.js + rails + docker
Eliminate Rails FatModel with value object
Recognize Rails projects with Intellij idea
[Grover] Generate PDF with Rails [2020 version]
Build Rails environment with Docker Compose
Track Rails app errors with Sentry
[Environment construction with Docker] Rails 6 & MySQL 8
Connect to Rails server with iPhone
How to get along with Rails
Create My Page with Rails devise
Introducing React to Rails with react-rails
Initial data input with [Rails] seed_fu!
Timeless search with Rails + JavaScript (jQuery)
Let's unit test with [rails] Rspec!