Rails 4添加file_field以将附件上传到现有表单和控制器 [英] Rails 4 add file_field for attachment upload to existing form and controller
问题描述
我是Rails的新手.现在已经学习了几个星期.请原谅我的白痴.我无法选择选择上传的文件.
I'm super new to rails. Been learning a few weeks now. Please excuse my idiocy. I cannot get my file I've selected to upload.
我正在使用Rails 4.0.0.
I'm using Rails 4.0.0.
我正在第一个应用程序上工作,我首先遵循了博客应用程序的rails指南.我接受并运行了它,并尝试尝试学习如何创建一个与众不同的(错误跟踪系统).
I am working on my first application, and I started by following the rails guide for the blog application. I took that and ran with it and am creating something different (bug tracking system) just trying to learn to ropes.
所以,我有自己的表格:
So, I've got my form:
<%= form_for @post do |f| %>
,并且我已经在file_field中添加了.从视图中看的显示部分在选择文件方面看起来和效果都很好.
and I've added in my file_field. The display part in from the view looks and works good as far as selecting a file goes.
<%= f.label :attachment %>
<%= f.file_field :attachment %>
我已将其从导轨4上仅供参考.所以我的控制器看起来像这样:
I've pulled this from the rails 4 guides FYI. So my controller looks like this:
class PostsController < ApplicationController
def new
@post = Post.new
end
def create
@post = Post.new(params[:post].permit(:title, :text, :user, :screen))
if @post.save
redirect_to posts_path
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.all
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :text, :user, :screen))
redirect_to posts_path
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
def upload
uploaded_io = params[:post][:attachment]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
end
private
def post_params
params.require(:post).permit(:title, :text, :user, :screen, :attachment)
end
end
此处的新片段已上传.通过数据库进行写入/读取以及显示其他所有内容,都可以正常工作.当显示视图时,我输入文本,附加一个文件,然后单击提交".一切都写入数据库并显示在索引上,但是我尝试附加的文件未写入〜/bugs/public/uploads/
The new piece in here is upload. Everything else is working fine with writing/reading from the database and displaying. When the view is displayed I make text entries, attached a file and hit submit. Everything writes to the database and shows up on index, but the file I've attempted to attached does not get written to ~/bugs/public/uploads/
预先感谢您的帮助.
推荐答案
我认为问题可能是::attachment属性不是创建"或更新"操作中的允许参数.
I think the problem might be that the :attachment attribute is not a permitted parameter in the "create" or "update" action.
我执行简单文件上传的方法是使用 Paperclip gem-此 railscast 解释得很好.真的很容易使用.
The way I do simple file uploads is with the Paperclip gem - this railscast explains it well. It's really easy to use.
还有此答案可以回答该问题.
此外,使用强参数的标准方法是在私有方法中定义允许的参数,并在控制器操作中调用该方法(因此您不必重复自己).这可能是导致错误的原因.
Also, the standard way to use strong parameters is defining permitted params in a private method and calling that method in the controller action (so you don't have to repeat yourself). That might be the cause of the error.
示例:
def create
@post = Post.new(post_params)
...
end
希望有帮助.
这篇关于Rails 4添加file_field以将附件上传到现有表单和控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!