Guest User

Untitled

a guest
Nov 24th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.17 KB | None | 0 0
  1. // Controller
  2.  
  3.   def create
  4.     @doc = Doc.new(doc_params)
  5.  
  6.     upload = params[:file]  
  7.    
  8.     @doc.path = "public/files/#{upload.original_filename}"
  9.     File.open(@doc.path, "wb") { |f| f.write(upload['datafile'].read)}  
  10.  
  11.     respond_to do |format|
  12.       if @doc.save
  13.         format.html { redirect_to @doc, notice: 'Doc was successfully created.' }
  14.         format.json { render :show, status: :created, location: @doc }
  15.       else
  16.         format.html { render :new }
  17.         format.json { render json: @doc.errors, status: :unprocessable_entity }
  18.       end
  19.     end
  20.   end
  21.  
  22.  
  23. // View
  24.  
  25. <%= form_for(@doc) do |f| %>
  26.   <% if @doc.errors.any? %>
  27.     <div id="error_explanation">
  28.       <h2><%= pluralize(@doc.errors.count, "error") %> prohibited this doc from being saved:</h2>
  29.  
  30.       <ul>
  31.       <% @doc.errors.full_messages.each do |message| %>
  32.         <li><%= message %></li>
  33.       <% end %>
  34.       </ul>
  35.     </div>
  36.   <% end %>
  37.  
  38.   <div class="field">
  39.     <%= f.label :title %><br>
  40.     <%= f.text_field :title %>
  41.   </div>
  42.   <div class="field">
  43.       <%= file_field_tag :file %>
  44.   </div>
  45.   <div class="actions">
  46.     <%= f.submit %>
  47.   </div>
  48. <% end %>
Advertisement
Add Comment
Please, Sign In to add comment