简体   繁体   English

Rails 4将附件上传的file_field添加到现有表单和控制器

[英]Rails 4 add file_field for attachment upload to existing form and controller

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. 我无法获取我选择上传的文件。

I'm using Rails 4.0.0. 我正在使用Rails 4.0.0。

I am working on my first application, and I started by following the rails guide for the blog application. 我正在处理我的第一个应用程序,我开始遵循博客应用程序的rails指南。 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| %>

and I've added in my file_field. 我已经在我的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 %>

I've pulled this from the rails 4 guides FYI. 我已经从导轨4指南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

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/ 一切都写入数据库并显示索引,但我试图附加的文件没有写入〜/ bugs / public / uploads /

Thanks in advance for the help. 在此先感谢您的帮助。

I think the problem might be that the :attachment attribute is not a permitted parameter in the "create" or "update" action. 我认为问题可能是:attachment属性不是“创建”或“更新”操作中的允许参数。

Edit: The way I do simple file uploads is with the Paperclip gem - this railscast explains it well. 编辑:我做简单文件上传的方式是使用Paperclip gem - 这个railscast很好地解释了它。 It's really easy to use. 它真的很容易使用。

There's also this answer that might answer the question. 还有这个答案可能会回答这个问题。

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. 这可能是错误的原因。

Example: 例:

def create
  @post = Post.new(post_params)
  ...
end

Hope that helps. 希望有所帮助。

I had the same problem, 我有同样的问题,

solution :- 解决方案: -

delete the "def upload" and give the code inside "def create" itself 删除“def upload”并将代码提供给“def create”本身

 > def create > > 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 > > @post = Post.new(params[:post].permit(:title, :text, :user, :screen)) > > if @post.save > redirect_to posts_path > else > render 'new' > end > end 

Make sure that the 'uploads' directory exists in 'public'. 确保“上传”目录存在于“公共”中。

You can handle this automatically by adding this line before the file operation: 您可以通过在文件操作之前添加此行来自动处理:

Dir.mkdir 'public/uploads' unless File.directory? 'public/uploads'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM