如何在rails 4中上传照片?

时间:2014-06-12 07:16:31

标签: ruby-on-rails ruby ruby-on-rails-3

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

Mac OS 10.9.3 Rails 4.1.0

现在我想将照片上传添加到我的网站!这样,在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
  6. 方案如下:

    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
    

    最后,添加&#34;上传照片功能&#34;在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>
    
               

    然而,我遇到了我无法理解的问题=&gt;

      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'
    

    你介意帮我解决问题吗?

1 个答案:

答案 0 :(得分:2)

<强>路线

以供将来读者参考,这是您的错误:

undefined method `product_photos_path'

-

如评论中所述,此处的问题是您引用的nested route不存在。您需要在路线中执行以下操作:

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

这将为您提供所需的路线。您目前正在使用namespace,这意味着您必须引用类似admin_product_photos_path

的内容

<强>回形针

您应该考虑的其他事项是paperclip

这是一款可以帮助您上传的宝石。将文件保存到数据库。它与现在的工作方式大致相同,只是它使用自己的datatable结构,并在模型中调用以定义对象:

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

这将允许您像这样调用image对象:

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

如果您想了解更多信息,我建议您使用this Railscast

enter image description here