Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 4 add file_field for attachment upload to existing form and controller
    primarykey
    data
    text
    <p>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.</p> <p>I'm using Rails 4.0.0.</p> <p>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.</p> <p>So, I've got my form:</p> <pre><code>&lt;%= form_for @post do |f| %&gt; </code></pre> <p>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.</p> <pre><code>&lt;%= f.label :attachment %&gt; &lt;%= f.file_field :attachment %&gt; </code></pre> <p>I've pulled this from the rails 4 guides FYI. So my controller looks like this:</p> <pre><code>class PostsController &lt; 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 </code></pre> <p>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/</p> <p>Thanks in advance for the help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload