【发布时间】:2015-02-25 11:11:54
【问题描述】:
您好,我是 ruby on rails 的初学者,我正在制作应用程序,我们可以在其中上传然后显示用户的图像,但图像没有上传和显示。 这是我的代码:
我的上传页面代码是:
<%= form_for @userinfo , :html => { :multipart => true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.file_field :photo , class: 'form-control' %>
<%= f.submit "Save Details", class: "btn btn-primary" %>
<% end %>
我的显示页面代码是:
<%= image_tag @userinfo.photo.url %>
<p>
<strong>Name:</strong>
<%= @userinfo.name %>
</p>
我的型号代码是:
class Userinfo < ActiveRecord::Base
has_attached_file :photo
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
我的控制器代码是:
class UserinfoController < ApplicationController
def new
@userinfo= Userinfo.new
end
def create
@userinfo = Userinfo.new(user_params)
if @userinfo.save
flash[:success] = "User created Successfully!!"
redirect_to userinfo_path
else
render 'new'
end
end
def show
@userinfo = Userinfo.find(params[:id])
end
private
def user_params
params.require(:userinfo).permit(:name, :father_name, :mother_name, :contact_number, :photo)
end
end
我从日志文件中得到的错误是:“ActionController::RoutingError (No route matches [GET] "/photos/original/missing.png")”并且在显示页面上它显示“missing”而不是显示图片。 我是初学者,所以告诉我我做错了什么。谢谢。
【问题讨论】:
-
哪个页面出错了?如果是显示页面,那么您是否也可以包含来自控制器的显示操作代码?
-
我们是否必须将
has_attached_file属性传递给params.require(:userinfo).permit -
你没有在模型中指定:url和:path?
-
@MaxWilliams:我在显示页面上收到错误消息。它不显示图像,而是显示“缺少”他们的。
-
@Deema:我必须指定哪个 url 和路径以及如何指定?能详细点吗?