[英]How to upload photos in rails 4?
https://gist.github.com/Gtar69/27139cee82c75992ed82
Mac OS 10.9.3 Rails 4.1.0
現在我想將照片上傳到我的網站! 這樣,在終端=>
方案如下:
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
最后,在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>
但是,我遇到了我無法理解的問題=>
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'
您介意幫助我解決問題嗎?
路線
供以后的讀者參考,這是您的錯誤:
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
如果您想了解更多信息,我建議您使用以下Railscast :
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.