我有一個項目模型has_many Photos(屬於項目),這兩個名稱在Admin下都有。 通過窗體上傳通過Photo的新操作或編輯操作按預期工作(但不是從Item顯示頁面渲染時,我得到一個「未定義的方法`model_name'爲NilClass:Class」錯誤。當我創建一個新的照片記錄我通過在ITEM_ID,我不知道該怎麼進入到項目頁面上的形式在rails中的不同控制器內渲染表單部分
PhotosController;
class Admin::PhotosController < Admin::BaseController
def new
@photo = Photo.new(:item_id => params[:item_id])
end
def create
@photo = Photo.new(params[:photo])
if @photo.save
flash[:notice] = "Successfully created photo."
redirect_to [:admin,@photo.item]
else
render :action => 'new'
end
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
if @photo.update_attributes(params[:photo])
flash[:notice] = "Successfully updated photo."
redirect_to [:admin,@photo.item]
else
render :action => 'edit'
end
end
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
flash[:notice] = "Successfully destroyed photo."
redirect_to [:admin,@photo.item]
end
end
照片/ _form.html.haml;
= form_for [:admin,@photo], :html => {:multipart => true,id: 'upload'} do |f|
= f.hidden_field :item_id
%p
= f.label :name
%br/
= f.text_field :name
%p
= file_field_tag :image, multiple: true, name:'photo[image]',id: 'photo_image'
%p= f.submit
items/show.html.haml;
= render 'admin/photos/form'