[英]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 我的控制器中有一个创建自定义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() 在我的主视图中,我编辑一个li,以重定向到我的generate_qrcode()
index.html.erb 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 但是,当我尝试生成qr时,我一直收到此错误消息
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. 这是因为控制器期望您返回与动作名称相同的html模板。
What you can do is create a new view with the same name as the action ( views/generate_qrcode.html.erb ). 您可以做的是创建一个与操作名称相同的新视图( 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 :
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: 我建议以私有方法生成QR代码,然后从控制器操作中呈现该代码:
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'.
就您而言,您可以将您的qrcode或图像作为实例变量,然后将其传递到名为“ 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.
您可以在索引操作中生成所有qr代码模态,或使用Ajax按需生成它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.