我有一个模型,它附加了一个图像,我想通过控制器将其传递到视图中。这是我自己尝试过的方式,但它不起作用:
模型:
class CustomForm < ActiveRecord:Base
has_attached_file :background_image,
:storage => :s3,
:bucket => "asdf",
...
belongs_to :project
控制器:
class FormsController < ApplicationController
before_filter :get_project
def show
@form = @project.forms.find(params[:id])
@bg_image = @form.background_image.url
end
def get_project
@project = current_account.projects.find(params[:project_id])
end
看法:
<html>
<head>
</head>
<body>
<%= image_tag @bg_image %>
</body>
</html>
这会产生“无法将 nil 转换为字符串”错误并指向“<%= image_tag @bg_image %>”行,我假设 @bg_image 为 nil。那么我在这里做错了什么?