Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## image.rb
- class Image < ActiveRecord::Base
- belongs_to :article
- has_attached_file :image, :styles => { :thumb => "100x100#", :standard => "300x200#" }
- end
- ## images_controller.rb
- class ImagesController < ApplicationController
- def new
- @article = Article.find(params[:id])
- @image = @article.images.new
- end
- def show
- @image = Image.find(params[:id])
- end
- def create
- @image = Image.new(params[:image])
- @image.user_id = session[:user_id]
- if @image.save
- flash[:notice] = "Image successfully uploaded"
- redirect_to :controller => 'articles', :action => 'show', :id => @image.article
- else
- flash.now[:error] = "Image could not be uploaded"
- render :action => 'new'
- end
- end
- end
- ## new.html.erb
- <% form_for :image, :html => { :multipart => true } do |form| -%>
- <%= form.error_messages %>
- <p><%= form.file_field :image %></p>
- <%= form.hidden_field :article_id %>
- <p><%= form.submit :create %></p>
- <% end -%>
Add Comment
Please, Sign In to add comment