Ruby on rails 照片未从控制器上载到数据库中

Ruby on rails 照片未从控制器上载到数据库中,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我检查了EDD代码是否正确,但我无法检测到为什么照片没有上传到数据库中的问题 class PhotosController < ApplicationController def new @photo = Photo.new end def create debugger @photo = Photo.new(photo_params) if @photo.save render "show", :notice=> "photo

我检查了EDD代码是否正确,但我无法检测到为什么照片没有上传到数据库中的问题

class PhotosController < ApplicationController
  def new
    @photo = Photo.new
end 

  def create
    debugger
    @photo = Photo.new(photo_params)
    if @photo.save
     render "show", :notice=> "photo created"
   else   
     render "index", :notice=> "photo could'nt be saved"  
   end

  end
def photo_params
   params.require(:photo).permit(:image)
 end
 end
我对photonew的看法如下

 <%= form_for :photo , :url=>{:action=>'create'},:html => { :multipart => true } do |f| %>
 <%= f.file_field :image %>
 <%= f.submit "Save" %>
<% end %>   
这是我的照片模型

 class Photo < ActiveRecord::Base
   belongs_to :user
   has_many :comments
   has_many :photos_tags
   has_many :tags,  through: :photos_tags
   has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
   validates :user, presence:true
   validates_associated :user
   validates_attachment_presence :image
   validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
  end
Thanx提前获取帮助。

模型中的“属于”关系会在照片表中生成一个“用户id”列,您得到的错误“用户不能为空”应该来自此,因为我看不到您将照片与上传用户关联的位置

如果您已经有了当前的用户帮助程序,如果您正在使用Desive,如果没有,您可能需要设置它,您可以查找此项,以便回答:,您可以执行以下操作:

def create
    @photo = Photo.new(photo_params)

    @photo.user = current_user 
    #it should populate the user_id column accordingly if current_user is a User object

    if @photo.save
     render "show", :notice=> "photo created"
   else   
     render "index", :notice=> "photo could'nt be saved"  
   end
end

可能还有另一种方法,但我认为这是非常简单的。

你能发布生成的服务器日志吗?@pavan实际上没有服务器日志。我检查了我的数据库。它的empy@RichPeck我添加此选项是为了检查当前执行点photo_params是否可以有任何值,它是否有photoyes参数它给出了错误验证失败:在我更改为if@photo.save后,用户不能为空!嗯,请发布你的照片模型和验证。太好了!记住将此帖子标记为结束此问题的答案。