2016-05-05 33 views
-1

修改'contacts controller'中的代碼從@contacts = Contact.all@contacts = current_user.contact,這樣用戶只能看到他們的聯繫人(目前任何用戶都可以看到所有聯繫人)這會引發下面的錯誤。嘗試調整,但仍然沒有成功,並在psql中檢查了數據庫,並且都有id列。代碼中需要的任何想法或修正?Rails關聯用戶和聯繫人投擲錯誤

Failure/Error: <% if @contacts.any? %> ActionView::Template::Error: PG::UndefinedColumn: ERROR: column contacts.user_id does not exist LINE 1: SELECT 1 AS one FROM "contacts" WHERE "contacts"."user_id" ...^: SELECT 1 AS one FROM "contacts" WHERE "contacts"."user_id" = $1 LIMIT 1

聯繫人控制器 類ContactsController < ApplicationController中

before_action :contact, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! 


    def index 
    @contacts = current_user.contact end 

    def new 
    @contact = Contact.new end 

    def create 
    Contact.create(contact_params) 
    redirect_to '/contacts' end 

    def show end 

    def edit end 

    def update 
    @contact.update(contact_params) 
    redirect_to '/contacts/' + "#{@contact[:id]}" end 


    def destroy 
    @contact.destroy 
    redirect_to '/contacts' end 

    private 

    def contact_params 
    params.require(:contact).permit(:firstname, :surname, :email, :phone, :image) end 

    def contact 
    @contact = Contact.find(params[:id]) end 


end 

用戶控制器

class UsersController < ApplicationController 

end 

聯繫模型

class Contact < ActiveRecord::Base 

    belongs_to :user 

    has_attached_file :image, styles: {thumb: "100x100>"} 
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ 

end 

用戶模型

class User < ActiveRecord::Base 
    has_many :contacts, dependent: :destroy 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
end 

指數HTML

<%if user_signed_in? %> 
    <%= link_to 'Log out', destroy_user_session_path, method: :delete %> 
<%end%> 

<% if @contacts.any? %> 
    <% @contacts.each do |contact| %> 
    <%= link_to image_tag(contact.image.url(:thumb)), contact_path(contact) %> 
    <h3><%= contact.firstname%> <%=contact.surname%></h3> 
    <%=contact.email%><br /> 
    <%=contact.phone%> 
    <br /> 
    <br /> 
    <%end%> 
<%else%> 
    No contacts yet! 
<%end%> 
<br /> 
<br /> 
<%= link_to 'Add a contact', new_contact_path%> 

架構

ActiveRecord::Schema.define(version: 20160504125849) do 

    # These are extensions that must be enabled in order to support this database 
    enable_extension "plpgsql" 

    create_table "contacts", force: :cascade do |t| 
    t.string "firstname" 
    t.string "surname" 
    t.string "email" 
    t.integer "phone" 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
    t.string "image_file_name" 
    t.string "image_content_type" 
    t.integer "image_file_size" 
    t.datetime "image_updated_at" 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.inet  "current_sign_in_ip" 
    t.inet  "last_sign_in_ip" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 

end 
+0

'PG :: UndefinedColumn:錯誤:列contacts.user_id不存在' - 錯誤已經足夠清楚了!你還沒有將'user_id'外鍵添加到'contacts'表 – dp7

+0

「錯誤已經足夠清楚了!」是完全不必要的。通常,一個人難倒的是另一個人。這就是我們提出問題的原因。 :P – jaydel

回答

1

基本上你沒有在聯繫人表中的user_id。這是用來定義與用戶的關係的外鍵。添加列,然後在創建聯繫人時,將用戶的ID添加到聯繫人表中的user_id列。那麼這應該工作。

+0

已將額外的user_id列添加到具有外鍵的聯繫人。現在在索引視圖中根本沒有任何聯繫人顯示,它沒有加載聯繫人url,但沒有顯示任何聯繫人,只有註銷或添加聯繫人鏈接。我不明白爲什麼。你有什麼想法? (重新運行遷移並重新啓動服務器&db) – blastoff

+1

這是因爲,對於舊記錄,您的user_id列爲零。 –

+0

@Gaurav Gupta爲什麼如果一個新用戶被創建(在所有修改並重新啓動服務器和db之後)以及在該用戶下添加了新聯繫人,它爲零?即使在這一切之後,索引顯示空白。 – blastoff