找不到id = 16的照片

时间:2013-11-29 05:43:40

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

不确定为什么这不起作用但我误导了我想...我宁愿不重新路线,只是将照片上传到当前的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     class UsersController< ApplicationController中

  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'

photo.rb

class Photo < ActiveRecord::Base
  attr_accessible :file

  belongs_to :attachable, :polymorphic => true

  mount_uploader :file, FileUploader
end

user.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

1 个答案:

答案 0 :(得分: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是一个数字,而不是一个字符串?

  3. 多态协会

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

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

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

    由于多种原因,此查询非常糟糕:

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

    2. 您尝试.find在关系(current_user.photos

    3. 之上

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

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

      然后您可以执行所需的更新