Guest User

Untitled

a guest
Jun 22nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. ## image.rb
  2.  
  3. class Image < ActiveRecord::Base
  4. belongs_to :article
  5.  
  6. has_attached_file :image, :styles => { :thumb => "100x100#", :standard => "300x200#" }
  7. end
  8.  
  9. ## images_controller.rb
  10. class ImagesController < ApplicationController
  11. def new
  12. @article = Article.find(params[:id])
  13. @image = @article.images.new
  14. end
  15.  
  16. def show
  17. @image = Image.find(params[:id])
  18. end
  19.  
  20. def create
  21. @image = Image.new(params[:image])
  22. @image.user_id = session[:user_id]
  23. if @image.save
  24. flash[:notice] = "Image successfully uploaded"
  25. redirect_to :controller => 'articles', :action => 'show', :id => @image.article
  26. else
  27. flash.now[:error] = "Image could not be uploaded"
  28. render :action => 'new'
  29. end
  30. end
  31. end
  32. ## new.html.erb
  33.  
  34. <% form_for :image, :html => { :multipart => true } do |form| -%>
  35. <%= form.error_messages %>
  36. <p><%= form.file_field :image %></p>
  37. <%= form.hidden_field :article_id %>
  38. <p><%= form.submit :create %></p>
  39. <% end -%>
Add Comment
Please, Sign In to add comment