Rails 4 add file_field to load the application into the existing form and controller

I am a super newbie in rails. Studied in a few weeks. Sorry, my idiocy. I can not get the file that I selected to download.

I am using Rails 4.0.0.

I am working on my first application, and I started with a rail guide for the blog application. I took this and ran with it and create something else (bug tracking system), just trying to learn the ropes.

So, I have my form:

<%= form_for @post do |f| %> 

and I added in the file_field field. The displayed part in the view looks and works well, as the file selection goes.

 <%= f.label :attachment %> <%= f.file_field :attachment %> 

I pulled it out of the rails of 4 FYI guides. 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 part is loading here. Everything else works fine with writing / reading from the database and displaying. When the view is displayed, I make text entries, attach the file and click submit. Everything is written to the database and displayed by index, but the file I tried to connect is not written to ~ / bugs / public / uploads /

Thanks in advance for your help.

+4
source share
3 answers

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

Edit: The way to upload files using Paperclip gem - this railscast explains it well. It is really easy to use.

There is also this answer that can answer the question.

In addition, the standard way to use strong parameters is to determine the allowed parameters in a private method and call this method in a controller action (so you don't need to repeat yourself). This may be the cause of the error.

Example:

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

Hope this helps.

+4
source

I had the same problem

: -

delete "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 
+2
source

Make sure the directory 'uploads' 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'

+1
source

Source: https://habr.com/ru/post/1498639/


All Articles