Skip to content

Instantly share code, notes, and snippets.

@Gtar69
Created June 12, 2014 06:30
Show Gist options
  • Save Gtar69/27139cee82c75992ed82 to your computer and use it in GitHub Desktop.
Save Gtar69/27139cee82c75992ed82 to your computer and use it in GitHub Desktop.
Mac OS 10.9.3
Rails 4.1.0
目前想增加一個上傳圖片給產品用的功能到網站後台
$ rails g model Photo image_name:string product:references
$ rake db:migrate
$ rails g migration add_image_to_photos image:string
$ rake db:migrate
$ 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>
<% end %>
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
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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment