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

牧云@^-^@ 提交于 2019-12-10 15:11:57

问题


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.

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| %>

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 %>

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

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/

Thanks in advance for the help.


回答1:


I think the problem might be that the :attachment attribute is not a permitted parameter in the "create" or "update" action.

Edit: The way I do simple file uploads is with the Paperclip gem - this railscast explains it well. 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.




回答2:


I had the same problem,

solution :-

delete the "def upload" and give the code inside "def create" itself

>  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



回答3:


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'



来源:https://stackoverflow.com/questions/18407013/rails-4-add-file-field-for-attachment-upload-to-existing-form-and-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!