2013-10-13 117 views
1

我有类似下面的嵌套的资源:用户/ 1 /照片/新轨道4强参数嵌套资源

resources :users, only: [] do 
    resources :photos, except: [:show] 
end 

我的form_for是这样的:

= form_for([@user, @photo], html: { multipart: true }) do |f| 

    .inputs 
    = f.file_field :attached_photo 

    .actions 
    = f.submit :submit 

我相信问题我有是具有较强的参数:

def photo_params 
     params.require(:photo).permit(:title, :attached_photo) 
    end 

当我打的形式我得到以下错误提交按钮:

的ActionController :: ParameterMissing在PhotosController#创建 PARAM未发现:照片

我用回形针所以在模型中我有:

has_attached_file :attached_photo 

请指教。

+0

什么是您的轨道服务器输出秀PARAMS贴? – jvperrin

+0

如果我为标题添加文本字段。然后参数如下:“photo”=> {“title”=>“”},“commit”=>“submit”,“painter_id”=>“1”看起来:attached_photo没有被传递。 – Brian

+0

即使您使用文件字段上传文件,该文件是否仍然无法传递?我在我的一个应用程序上使用回形针照片上传进行了测试,并且只有在文件字段实际选择了文件时才会包含该参数。 – jvperrin

回答

2

如果你想确保用户上传照片提交此表时,那么你应该在Photo模型添加验证,像这样:

validates_attachment_presence :attached_photo 

然后,如果用户没有上传照片,表单将重新呈现,告诉用户上传照片。

你也想利用这个强大的参数的代码,以确保你没有一个错误,如果photo参数不存在:

def photo_params 
    params.require(:photo).permit(:title, :attached_photo) if params[:photo] 
end 
+0

修好了!谢谢 – Brian