2016-05-12 9 views
0

Ich bin ziemlich neu auf RoR.Erstellen Sie eine 1: 1-Beziehung zwischen Benutzer und einem Profil

Ich habe zwei Modelle, einen Benutzer (generiert von Devise) und ein Profil.

Ich möchte ein Profil pro Benutzer haben.

Hier sind meine User Stories:

Als Benutzer muss ich, erstellen Sie ein Profil Als Nutzer kann ich mein Profil bearbeiten Als Nutzer kann ich, sehen alle Profile

Below Du wirst meine zwei verschiedenen Modelle sehen.

Ich weiß nicht warum, aber heute kann ein Benutzer viele Profile erstellen und ein anderes Profil bearbeiten.

Kann mir jemand helfen zu verstehen, warum?

+0

Ich denke, das Problem ist nicht in den Modellen, sondern in der Steuerung, zeigen Sie uns einige mehr Code – Fred

+0

Ja, zeig uns die Controller und auch dein schema.rb im DB Ordner bitte – RuNpiXelruN

+0

Siehe unten Schema und Profil Controller –

Antwort

1

Um Benutzer zu verhindern, dass andere Profil bearbeiten Sie so etwas wie dies in Ihrem Profil-Controller Update-Aktion

if current_user == @profile.user 
allow to edit 
else 
don't allow to edit 
+0

! Vielen Dank für Ihre Antwort! Aber könnten Sie bitte einen Blick auf meinen Code? Ich habe immer noch die pb –

0

Hier ist meine schema.rb

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

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

    create_table "attachinary_files", force: :cascade do |t| 
    t.string "attachinariable_type" 
    t.integer "attachinariable_id" 
    t.string "scope" 
    t.string "public_id" 
    t.string "version" 
    t.integer "width" 
    t.integer "height" 
    t.string "format" 
    t.string "resource_type" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "attachinary_files", ["attachinariable_type", "attachinariable_id", "scope"], name: "by_scoped_parent", using: :btree 

    create_table "bookings", force: :cascade do |t| 
    t.integer "user_id" 
    t.integer "profile_id" 
    t.boolean "status" 
    t.date "teetime" 
    t.text "message" 
    end 

    add_index "bookings", ["profile_id"], name: "index_bookings_on_profile_id", using: :btree 
    add_index "bookings", ["user_id"], name: "index_bookings_on_user_id", using: :btree 

    create_table "mailboxer_conversation_opt_outs", force: :cascade do |t| 
    t.string "unsubscriber_type" 
    t.integer "unsubscriber_id" 
    t.integer "conversation_id" 
    end 

    add_index "mailboxer_conversation_opt_outs", ["conversation_id"], name: "index_mailboxer_conversation_opt_outs_on_conversation_id", using: :btree 
    add_index "mailboxer_conversation_opt_outs", ["unsubscriber_id", "unsubscriber_type"], name: "index_mailboxer_conversation_opt_outs_on_unsubscriber_id_type", using: :btree 

    create_table "mailboxer_conversations", force: :cascade do |t| 
    t.string "subject", default: "" 
    t.datetime "created_at",    null: false 
    t.datetime "updated_at",    null: false 
    end 

    create_table "mailboxer_notifications", force: :cascade do |t| 
    t.string "type" 
    t.text  "body" 
    t.string "subject",    default: "" 
    t.string "sender_type" 
    t.integer "sender_id" 
    t.integer "conversation_id" 
    t.boolean "draft",    default: false 
    t.string "notification_code" 
    t.string "notified_object_type" 
    t.integer "notified_object_id" 
    t.string "attachment" 
    t.datetime "updated_at",       null: false 
    t.datetime "created_at",       null: false 
    t.boolean "global",    default: false 
    t.datetime "expires" 
    end 

    add_index "mailboxer_notifications", ["conversation_id"], name: "index_mailboxer_notifications_on_conversation_id", using: :btree 
    add_index "mailboxer_notifications", ["notified_object_id", "notified_object_type"], name: "index_mailboxer_notifications_on_notified_object_id_and_type", using: :btree 
    add_index "mailboxer_notifications", ["sender_id", "sender_type"], name: "index_mailboxer_notifications_on_sender_id_and_sender_type", using: :btree 
    add_index "mailboxer_notifications", ["type"], name: "index_mailboxer_notifications_on_type", using: :btree 

    create_table "mailboxer_receipts", force: :cascade do |t| 
    t.string "receiver_type" 
    t.integer "receiver_id" 
    t.integer "notification_id",       null: false 
    t.boolean "is_read",     default: false 
    t.boolean "trashed",     default: false 
    t.boolean "deleted",     default: false 
    t.string "mailbox_type", limit: 25 
    t.datetime "created_at",         null: false 
    t.datetime "updated_at",         null: false 
    t.boolean "is_delivered",    default: false 
    t.string "delivery_method" 
    t.string "message_id" 
    end 

    add_index "mailboxer_receipts", ["notification_id"], name: "index_mailboxer_receipts_on_notification_id", using: :btree 
    add_index "mailboxer_receipts", ["receiver_id", "receiver_type"], name: "index_mailboxer_receipts_on_receiver_id_and_receiver_type", using: :btree 

    create_table "profiles", force: :cascade do |t| 
    t.string "first_name" 
    t.string "last_name" 
    t.string "handicap" 
    t.string "postbox" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "tagline" 
    t.string "skills" 
    t.string "town" 
    t.integer "user_id" 
    t.float "latitude" 
    t.float "longitude" 
    t.string "street" 
    end 

    add_index "profiles", ["user_id"], name: "index_profiles_on_user_id", using: :btree 

    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 
    t.string "prenom" 
    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 

    add_foreign_key "bookings", "profiles" 
    add_foreign_key "bookings", "users" 
    add_foreign_key "mailboxer_conversation_opt_outs", "mailboxer_conversations", column: "conversation_id", name: "mb_opt_outs_on_conversations_id" 
    add_foreign_key "mailboxer_notifications", "mailboxer_conversations", column: "conversation_id", name: "notifications_on_conversation_id" 
    add_foreign_key "mailboxer_receipts", "mailboxer_notifications", column: "notification_id", name: "receipts_on_notification_id" 
    add_foreign_key "profiles", "users" 
end 

tun können, und hier ist das Profil Controller