Guest User

Untitled

a guest
Apr 27th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. ## With this code instead of passing a file and being a file object in the params like:
  2. ## Parameters: {"commit"=>"Save changes", "authenticity_token"=>"CNHWuiMsSkwK4x3biFGogUnRU2kutvKuCfOTH2Y+PO4=", "painting"=>{"image"=>#<File:/tmp/RackMultipart20100102-3131-9c0z0-0>}}
  3.  
  4. ## it passes just a name of the file like so:
  5.  
  6. ## Parameters: {"action"=>"create", "authenticity_token"=>"atvaDTdf2yhQKea8t/VBhYyT2ENyK9B/ZYGQSRq9414=", "painting"=>{"title"=>"g", "height"=>"1", "picture"=>"Tweetie.gif", "width"=>"1"}, "controller"=>"admin/paintings"}
  7.  
  8.  
  9. ## Model
  10.  
  11. class Painting < ActiveRecord::Base
  12.  
  13. has_attached_file :picture
  14.  
  15. validates_presence_of :title, :height, :width
  16. validates_attachment_presence :picture
  17.  
  18. end
  19.  
  20. ## Controller
  21.  
  22. class Admin::PaintingsController < AdminController
  23.  
  24. def new
  25. @painting = Painting.new
  26. end
  27.  
  28. def create
  29. @painting = Painting.new(params[:painting])
  30. if @painting.save
  31. redirect_to admin_paintings_path
  32. else
  33. render :new
  34. end
  35. end
  36.  
  37. end
  38.  
  39. ## View
  40.  
  41. - content_for :heading do
  42. %h2.head= "Add Painting"
  43.  
  44. - form_for @painting, :url => admin_paintings_path, :html => { :mulitpart => true } do |f|
  45. = f.error_messages
  46. %fieldset
  47. .input_field
  48. = f.label :title
  49. = f.text_field :title, :class => "input mediumfield"
  50. .input_field
  51. = f.label :height
  52. = f.text_field :height, :class => "input smallfield"
  53. %span (mm)
  54. .input_field
  55. = f.label :width
  56. = f.text_field :width, :class => "input smallfield"
  57. %span (mm)
  58. .input_field
  59. = f.label :picture
  60. = f.file_field :picture
  61.  
  62. %input{ :value => "Submit", :class => "submit", :type => "submit"}
  63.  
  64. %div{ :class => "clear"}
Add Comment
Please, Sign In to add comment