2016-05-04 12 views
1

最初は連絡先で開始され、期待通りにCRUDを正しく許可する人口連絡先のリストが作成されました。その後、ユーザーを作成したdeviseでログインを設定します。 2人の異なるユーザーとしてログインすると、それぞれが自分の連絡先のみが表示されるはずですが、現在のところ、ユーザーはすべて同じ連絡先が表示されます。Railsユーザと連絡先との関連での問題

この問題を解決するお手伝いがありますか?

(ユーザーで開始し、その後、連絡先を作成しているかどうかを確認しない、このように感じて後方に作成されます。)団体のための

理解はユーザーにhas_many:連絡先」で、連絡先「belongs_toの:ユーザーが」変更されました無駄にスパイク。

ContactsController

class ContactsController < ApplicationController 

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


    def index 
    @contacts = Contact.all 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 

がUserController

class UsersController < ApplicationController 

end 

model Contact 
class Contact < ActiveRecord::Base 

    belongs_to :users 

    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 

indexhtml

<%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%> 

ショーHTML

<p><%= image_tag @contact.image.url(:thumb) %></p> <p><%= @contact.firstname %> <%= @contact.surname %></p> <p><%= @contact.phone %></p> <p><%= @contact.email %></p> 

<%= link_to 'Edit', edit_contact_path(@contact) %> <%= link_to 'Remove', contact_path(@contact), method: :delete %><br /><br /> <%= link_to 'Contacts', contacts_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 

答えて

1

When logging in as two different users each should only see their own contacts however currently users sees all the same contacts

問題は、この行@contacts = Contact.allです。すべての連絡先を保持します。あなただけCURRENT_USERの連絡先を表示すると、あなただけに

@contacts = current_user.contacts 

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

の下にそれを必要とすることはcontacts表はuser_id列を持っていないようです。新しいマイグレーションを作成して同じものを追加してください。rake db:migrate

+0

復帰してくれてありがとうございます。コードを更新しましたが、新しいエラー 'Failure/Error:<%if @ contacts.any? ActionView :: Template :: Error: PG :: UndefinedColumn:エラー:列contacts.user_idが存在しません 行1:SELECT 1から1つの「連絡先」WHERE「連絡先」WHERE「連絡先」。「user_id」... ^ : "連絡先" WHERE "連絡先"から1つを選択してください。 "user_id" = $ 1 LIMIT 1' – blastoff

+0

@blastoff更新しました – Pavan

+0

誰でもアイデアはありますか? psql(user、contact)の両方のデータテーブルをチェックして、それぞれにidカラムが含まれていることを確認してください。まだエラーが発生しています。エラー/エラー:<%if @ contacts.any? %1> SELECT 1 1 "FROM" contacts "WHERE" contacts "。" user_id "... ^:SELECT 1:SELECT 1:アクションビュー::テンプレート::エラー:PG :: UndefinedColumn:エラー:列contacts.user_idが存在しません。 "連絡先"から "連絡先"から1つ "user_id" = $ 1 LIMIT 1 – blastoff

関連する問題