我遇到了用户可下载文件的奇怪问题,文件正在正常下载,但控制器操作中的其他代码正在执行两次.我正在使用CarrierWave,它安装在.file的Document模型上.
我希望用户能够单击此链接下载文件:
<%= link_to document.file.file.filename, document_path(document) %>
Run Code Online (Sandbox Code Playgroud)
这就是我的控制器的样子:
def show
document = Document.find(params[:id])
# Track this download
CourseDownload.create(course: document.course, user: current_user)
# Download the file
send_file 'public' + document.file.url.to_s
end
Run Code Online (Sandbox Code Playgroud)
单击该链接时,文件将下载,但会创建2个CourseDownload记录.在日志中,看起来GET请求发生了两次:
Started GET "/documents/1" for 127.0.0.1 at 2014-04-17 18:12:47 -0400
Processing by DocumentsController#show as HTML
Parameters: {"id"=>"1"}
Document Load (0.2ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxx' LIMIT 1
CACHE (0.0ms) SELECT "documents".* FROM …Run Code Online (Sandbox Code Playgroud) 我正在尝试避免管理表单和常规用户表单之间的代码重复相同的资源.
我希望能够通过以下方式使用一种形式:
<% if current_user.admin? %>
<%= form_for([:admin,@post], :html => {class: "form"}) do |f| %>
<% else %>
<%= form_for @post, html: { class: 'form' } do |f| %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后包括只有管理员应该通过表单正文中的语句看到的字段.
我认为这种方法不起作用,因为<%end%>正在结束表单.
有没有办法做到这一点?这种方法是否有意义?
谢谢!