简体   繁体   中英

Ruby On Rails Error 204 generate an image into a new view from controller

I have in my controller a method to create a custom qr

def generate_qrcode()
require 'rqrcode'
qrcode = RQRCode::QRCode.new('ejemplo')
image = qrcode.as_png(
        resize_gte_to: false,
        resize_exactly_to: false,
        fill: 'white',
        color: 'black',
        size: 120,
        border_modules: 4,
        module_px_size: 6,
        file: nil # path to write
        )
 return image.resize(150, 150)
end

and in my main view, i edit a li, to redirect to my generate_qrcode()

index.html.erb

<p id="notice"><%= notice %></p>

<h1>Beneficios</h1>

<table>
  <thead>
    <tr>
      <th>Id sucursal</th>
      <th>Nombre</th>
      <th>Estado</th>
      <th>Descripcion</th>
      <th>Tipo</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @beneficios.each do |beneficio| %>
      <tr>
        <td><%= beneficio.Id_sucursal %></td>
        <td><%= beneficio.Nombre %></td>
        <td><%= beneficio.Estado %></td>
        <td><%= beneficio.Descripcion %></td>
        <td><%= beneficio.Tipo %></td>
        <td><%= link_to 'Show', beneficio %></td>
        <td><%= link_to 'Edit', edit_beneficio_path(beneficio) %></td>
        <td><%= link_to 'Destroy', beneficio, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <td><%= link_to 'Qr', :controller => :beneficios, :action => :generate_qrcode %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Beneficio', new_beneficio_path %>

and in my routes

resources :beneficios do
    collection do
      get :generate_qrcode
    end
  end

however when i try to generate the qr, i keep receiving this error message

错误显示

i search for answers but i cannot find something that can help me to figure out this.

That's because the controller is expecing you to return a html template with the same name as the action.

What you can do is create a new view with the same name as the action ( views/generate_qrcode.html.erb ).

Save the qr code image in an instance variable:

def generate_qrcode()
require 'rqrcode'
qrcode = RQRCode::QRCode.new('ejemplo')
image = qrcode.as_png(
        resize_gte_to: false,
        resize_exactly_to: false,
        fill: 'white',
        color: 'black',
        size: 120,
        border_modules: 4,
        module_px_size: 6,
        file: nil # path to write
        )
 @qr_code_img = image.resize(150, 150)
end

Then populate the view you created in order to show the qr code image:

<%= image_tag @qr_code_img %>

To render raw binary data (such as an image) from a controller, use send_data :

send_data(image.to_s, type: 'image/png', disposition: 'inline')

I recommend generating your QR code in a private method, then rendering that from your controller action:

class BeneficiosController < ApplicationController
  require 'rqrcode'

  def generate_qrcode
    qrcode = make_qrcode
    send_data qrcode.to_s, type: 'image/png', disposition: 'inline'
  end

  private

  def make_qrcode
    qrcode = RQRCode::QRCode.new('ejemplo')
    image = qrcode.as_png(
        resize_gte_to: false,
        resize_exactly_to: false,
        fill: 'white',
        color: 'black',
        size: 120,
        border_modules: 4,
        module_px_size: 6,
        file: nil # path to write
        )
    image.resize(150, 150)
  end
end

You can't return an image object in controller to render it. You need to call a view template. In your case, you can make your qrcode or image an instance variable and pass it to a template named 'template_qrcode'.

Since you want to show qr code, it might be more user friendly to show it in a popup like a modal window rather than redirect user to a different page. You can generating all your qr code modals in your index action, or use Ajax to generate it on demand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM