Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 647
kyle Avatar asked Aug 23 '13 15:08

kyle


3 Answers

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.

like image 130
Chris C. Avatar answered Oct 12 '22 22:10

Chris C.


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
like image 37
Ballaji T Avatar answered Oct 12 '22 20:10

Ballaji T


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'

like image 27
Skofo Avatar answered Oct 12 '22 20:10

Skofo