Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Controller
- def create
- @doc = Doc.new(doc_params)
- upload = params[:file]
- @doc.path = "public/files/#{upload.original_filename}"
- File.open(@doc.path, "wb") { |f| f.write(upload['datafile'].read)}
- respond_to do |format|
- if @doc.save
- format.html { redirect_to @doc, notice: 'Doc was successfully created.' }
- format.json { render :show, status: :created, location: @doc }
- else
- format.html { render :new }
- format.json { render json: @doc.errors, status: :unprocessable_entity }
- end
- end
- end
- // View
- <%= form_for(@doc) do |f| %>
- <% if @doc.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@doc.errors.count, "error") %> prohibited this doc from being saved:</h2>
- <ul>
- <% @doc.errors.full_messages.each do |message| %>
- <li><%= message %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
- <div class="field">
- <%= f.label :title %><br>
- <%= f.text_field :title %>
- </div>
- <div class="field">
- <%= file_field_tag :file %>
- </div>
- <div class="actions">
- <%= f.submit %>
- </div>
- <% end %>
Advertisement
Add Comment
Please, Sign In to add comment