CWC
CWC

Reputation: 167

How to upload photos in rails 4?

https://gist.github.com/Gtar69/27139cee82c75992ed82

Mac OS 10.9.3 Rails 4.1.0

Now I want to add photo upload to my website ! In that way, in terminal=>

  1. $ rails g model Photo image_name:string product:references
  2. $ rake db:migrate
  3. $ rails g migration add_image_to_photos image:string
  4. $ rake db:migrate
  5. $ rails g controller Photos

Scheme as below:

ActiveRecord::Schema.define(version: 20140612034245) do

  create_table "photos", force: true do |t|
    t.string   "image_name"
    t.integer  "product_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image"
  end



config/routes.rb
Rails.application.routes.draw do
  devise_for :users
    namespace :admin do 
      resources :products do
       resources :photos
      end 
  end 
end



For new created controller, PhotosController shown below" 

class PhotosController < ApplicationController


  def create
    @product = Product.find(params[:product_id])
      @photo = @product.photos.create(photo_params)
    redirect_to admin_product_path(@product)    
  end

  private
    def photo_params
      params.require(:photo).permit(:image_name, :iamge)
    end     
end

Finally, adding "upload photos feature" in new.html.erb

<div class="panel panel-default">
  <div class="panel-body">

<%= simple_form_for [:admin, @product] do|f| %>   
    <div  class="group">
        <%= f.input :title, label:'標題' %>
    </div>

    <div class="group">
        <%= f.input :description, label:'敘述'%>  
    </div>      

    <div class = "group">
        <%= f.input :quantity, label:'數量' %>
    </div>

    <h2>Add Photos</h2>
    <%= form_for([@product, @product.photos.build]) do |f| %>
    <% end %>
    <%= f.submit "Submit", :disable_with => 'Submiting...' %>
  </div>
</div>

However, I encounter the problem I can't understand =>

  Rendered admin/products/new.html.erb within layouts/application (18.8ms)
Completed 500 Internal Server Error in 57ms

Showing /Users/Gtar/projects/artstore/app/views/admin/products/new.html.erb where line #26 raised:

undefined method `product_photos_path' for #<#<Class:0x000001013b2ef8>:0x00000101118670>

ActionView::Template::Error (undefined method `product_photos_path' for #<#<Class:0x000001013b2ef8>:0x00000101118670>):
    23:     </div>  form_for([@article, @article.comments.build]) do |f| %>
    24: -->
    25:     <h2>Add Photos</h2>
    26:     <%= form_for([@product, @product.photos.build]) do |f| %>
    27:     <% end %>
    28:     <%= f.submit "Submit", :disable_with => 'Submiting...' %>
    29:   </div>
  app/views/admin/products/new.html.erb:26:in `block in _app_views_admin_products_new_html_erb__2984465001892677316_2165881840'
  app/views/admin/products/new.html.erb:8:in `_app_views_admin_products_new_html_erb__2984465001892677316_2165881840'

Would you mind helping me for solve the issure?

Upvotes: 1

Views: 450

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

Routes

For reference for future readers, here's your error:

undefined method `product_photos_path'

--

The problem here, as stated in the comments, is you're referencing a nested route, which doesn't exist. You'll need to do something like this in your routes:

#config/routes.rb
#no namespace
resources :products do
   resources :photos #-> domain.com/products/1/photos
end 

This will give you the route you need. You're currently using a namespace, which means you'll have to reference something like admin_product_photos_path


Paperclip

Something else you should consider is paperclip

This is a gem which helps you upload & save files to your database. It works much the same as you have now, except it uses its own datatable structure, with a call in your model to define the object:

#app/models/photo.rb
Class Photo < ActiveRecord::Base
   has_attached_file :image
   validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

This will allow you to call the image object like this:

@product = Product.find params[:id]
@product.photos.first.image #-> image object from Paperclip

If you'd like more information, I would recommend you use this Railscast:

enter image description here

Upvotes: 2

Related Questions