2016-05-05 5 views
-1

@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 

Userモデル

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:ERROR:コラムcontacts.user_idはexist'ていません - エラーが十分に明確です!あなたは、「エラーが十分に明確である!」 – dp7

+0

'contacts'テーブルに' user_id'外部キーを追加していません全く必要ありません。一人の人が困惑したことは、しばしば他の人には明らかです。だからこそ私たちは質問します。 :P – jaydel

答えて

1

は、基本的には、連絡先テーブル内のuser_idを持っていません。これは、ユーザーとの関係を定義するために使用される外部キーです。列を追加して、連絡先を作成するときに、連絡先テーブルのuser_id列にユーザーのIDを追加します。それはうまくいくはずです。

+0

外部キーを持つ連絡先に追加のuser_id列を追加しました。インデックスビューに連絡先が一切表示されず、連絡先URLが読み込まれますが、連絡先は表示されず、ログアウトまたは連絡先リンクのみが表示されます。私は理由を見ることができません。あなたはなにか考えはありますか?古いレコードの列nilをUSER_IDている、ので、(移行を再実行し、サーバを再起動さ&DB) – blastoff

+1

それはあります。 –

+0

@ Gaurav Gupta新しいユーザーが作成された(すべての修正と再起動後のサーバーとデータベース)とそのユーザーの下に新しい連絡先が追加されたのはなぜですか?この後も、インデックスは空白になります。 – blastoff