【问题标题】:Couldn't find Photo with id=16找不到 id=16 的照片
【发布时间】:2013-11-29 05:43:39
【问题描述】:

不知道为什么这不起作用,但我认为我被误导了......我宁愿不重新路由,而只是将照片上传到当前的landing_welcome页面......不要转移到更新模板。

错误:

Couldn't find Photo with id=16 [WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2]

def update
@user = current_user.photos.find(params[:id])
@user.update_attributes!(person_params)
redirect_to @user
end

Users_controller.rb 类用户控制器

  def update
    @user = current_user.photos.find(params[:id])
    @user.update_attributes!
    redirect_to @user
  end

  def show

  end
end

landing_welcome.html.erb

 <div class="tab-pane" id="tab2">                                                                                                            
                  <%= nested_form_for current_user, :html=>{:multipart => true } do |f| %>                                                                    
                    <%= f.fields_for :photos do |p|  %>                                                                                                       
                    <p>                                                                                                                                       
                    <%= image_tag(p.object.file.url) if p.object.file? %>                                                                                     
                    <%= p.label :file %><br />                                                                                                                
                    <%= p.file_field :file %>                                                                                                                 
                    </p>                                                                                                                                      
                    <%= p.link_to_remove "Remove this attachment" %>                                                                                          
                    <% end %>                                                                                                                                 
                    <%= f.link_to_add "Add photos to your profile", :photos %>                                                                                
                    <br /><br />                                                                                                                              
                    <p><%= f.submit %></p>                                                                                                                    
                    <% end %>                                                                                                                                 
                  </div>         

routes.rb

root to: "home#landing"

  devise_for :users, :controllers => {:registrations => "users/registrations",
    :sessions => "users/sessions",
    :passwords => "users/passwords"}

   get "welcome", to: "home#landing_welcome"

  devise_scope :user do
  #  get "edit/edit_account", :to => "devise/registrations#edit_account", :as => "account_registration"                                                       
    get 'edit/edit_account' => 'users/registrations#account_registration', as: :edit_account
  end

  patch '/users/:id', to: 'users#update', as: 'user'

照片.rb

class Photo < ActiveRecord::Base
  attr_accessible :file

  belongs_to :attachable, :polymorphic => true

  mount_uploader :file, FileUploader
end

用户.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email,
                  :password,
                  :password_confirmation,
                  :zip,
                  :gender,
                  :remember_me,
                  :first_name,
                  :last_name,
                  :birthday,
                  :current_password,
                  :occupation,
                  :address,
                  :interests,
                  :aboutme,
                  :profile_image,
                  :photos_attributes

  has_many :photos, as: :attachable
  accepts_nested_attributes_for :photos
  mount_uploader :profile_image, ProfileImageUploader

  validates :gender, :presence => true

  def number_of_users
    User.all.count
  end

end

【问题讨论】:

  • id 不应该以数字开头。
  • @GaryHayes,你不是说 id 不应该以 $ 开头吗?

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

由于没有更好的答案,我认为您的问题在于您的应用生成的查询:

Couldn't find Photo with id=16 [WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2]

这里有两个因素:

  1. 为什么您的attachable_id 被称为$1
  2. 为什么您的attachable_type 是数字而不是字符串?

多态关联

您的查询尝试使用ID=16 加载Photo,但是,您的查询还尝试验证模型,以满足多态关联。这就是错误的来源

由于您没有说明此错误显示的是哪个路由/页面,我只能推测问题的原因:

@user = current_user.photos.find(params[:id])

这个查询真的很糟糕,原因有很多:

  1. 您正在直接使用current_user 对象。我在这里可能是错的,但是 Devise / Warden 使用它来存储有关登录用户的相关信息,并且不是真正的 ActiveRecord 对象(带有关系数据等)

  2. 您正在尝试在关系之上使用.find (current_user.photos)

虽然这可能不正确,但我会考虑为该查询执行此操作:

@photo = User.joins(:photos).find(current_user.id).where("photos.id = ?", params[:id])

然后你可以执行你需要的更新

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2014-10-23
    • 1970-01-01
    • 2011-11-05
    相关资源
    最近更新 更多