我在本文之后尝试将上传添加到我的rails项目:How To: Secure Upload。一切似乎工作正常,但我在索引视图上得到一个no方法错误:
ActionView::Template::Error (undefined local variable or method `f' for #<#<Class:0x007f690e634600>:0x007f690e620718>):
24:
25: <tr>
26: <td><%= resume.name %></td>
27: <td><%= link_to File.basename(f.resume.url), "/uploads/#{f.id}/#{File.basename(f.resume.url)}" %></td>
28: <td><%= button_to "Delete", resume, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{resume.name}?" %></td>
29: </tr>
30:
app / views / resumes / index.html.erb:27:in block in _app_views_resumes_index_html_erb___1440061793164452865_70044592937660'
app/views/resumes/index.html.erb:23:in
_ app_views_resumes_index_html_erb ___ 1440061793164452865_70044592937660&#39;
我的模特:
class Resume < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
validates :name, presence: true # Make sure the owner's name is present.
end
我的控制器:
class ResumesController < ApplicationController
def index
@resumes = Resume.all
end
def new
@resume = Resume.new
end
def create
@resume = Resume.new(resume_params)
if @resume.save
redirect_to resumes_path, notice: "The resume #{@resume.name} has been uploaded."
else
render "new"
end
end
def download
path = "/#{resume.resume}"
send_file path, :x_sendfile=>true
end
def destroy
@resume = Resume.find(params[:id])
@resume.destroy
redirect_to resumes_path, notice: "The resume #{@resume.name} has been deleted."
end
private
def resume_params
params.require(:resume).permit(:name, :attachment)
end
end
我的索引视图:
<% if !flash[:notice].blank? %>
<div class = "alert alert-info">
<%= flash[:notice] %>
</div>
<% end %>
<br />
<%= link_to "New Resume", new_resume_path, class: "btn btn-primary" %>
<br />
<br />
<table class = "table table-bordered table-striped">
<thead>.
<tr>
<th>Name</th>
<th>Download Link</th>
<th> </th>
</tr>
</thead>
<tbody>
<% @resumes.each do |resume| %>
<tr>
<td><%= resume.name %></td>
<td><%= link_to File.basename(f.resume.url), "/uploads/#{f.id}/#{File.basename(f.resume.url)}" %></td>
<td><%= button_to "Delete", resume, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{resume.name}?" %></td>
</tr>
<% end %>
</tbody>
</table>
我一直在谷歌搜索答案几个小时,似乎无法根据以前的答案追踪解决方案。如果有人被忽视,就道歉。
答案 0 :(得分:0)
在操作方法中有一个拼写错误(以及这个操作方法aslo http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm)。只需重命名@resumes - &gt; @resume在视图和控制器中。