是红宝石的新手。在这里,我试图显示数据库中的图像。为此,我利用此处找到的解决方案 link。但是当我运行脚本时,它会显示错误
No route matches {:action=>"show", :controller=>"attachments_controller", :id=>17}
请问我在这条路线上做错了什么。
路线
Rails.application.routes.draw do
resources :attachments, only: [:index, :new, :create, :destroy]
root "attachments#create"
get "attachments/show" => "attachments#show"
end
attachments_controller
class AttachmentsController < ApplicationController
def show
@attachment = Attachment.find(params[:id])
send_data @attachment.data, :filename => @attachment.filename, :type => @attachment.content_type
end
end
show.html
<%= image_tag url_for(:controller => "attachments_controller", :action => "show", :id => @attachment.id) %>
答案 0 :(得分:3)
您提供的错误消息指出:
No route matches {:action=>"show", :controller=>"attachments_controller", :id=>17}
您提供的路线文件显示了您创建的路线:
resources :attachments, only: [:index, :new, :create, :destroy]
get "attachments/show" => "attachments#show"
正在运行的耙路将显示您已经在第一行中创建了4条路,以及一条响应“附件/显示”的路。如果您确实要定义这样的路线,则应尝试:
get "attachments/:id", to: "attachments/show"
您的第一条路线仅对show一词作出响应,并且不提供任何参数。最后一条路线将采用附件之后的所有内容,并将其作为名为“ id”的参数传递给附件控制器的show action。
当然,最简单的方法就是摆脱所有这些,只需将第一条路线更改为:
resources :attachments, only: [:index, :new, :create, :destroy, :show]
让导轨为您创建放映路线与手动定义放映路线完全相同,显然阅读效果要好得多
答案 1 :(得分:0)
更改
<%= image_tag url_for(:controller => "attachments_controller", :action => "show", :id => @attachment.id) %>
到
<%= image_tag url_for(:controller => "attachments", :action => "show", :id => @attachment.id) %>