我对RoR很新。
我有两个模型,一个用户(由Devise生成)和一个配置文件。
我希望每个用户都有一个配置文件。
以下是我的用户故事:
作为用户,我必须创建个人资料 作为用户,我可以编辑我的个人资料 作为用户,我可以查看所有配置文件
下面,您将看到我的两种不同型号。
class Profile < ApplicationRecord
has_attachment :photo
belongs_to :user, class_name: 'User', foreign_key: :user_id
end
class User < ApplicationRecord
has_one :profile
devise :database_authenticatable, :registerable,
:rememberable, :trackable, :validatable
end
我不知道为什么,但今天,用户可以创建许多个人资料并编辑另一个个人资料。
有谁能帮助我理解为什么?
答案 0 :(得分:1)
为了防止用户编辑其他个人资料,您可以在个人资料控制器更新操作
中执行此类操作if current_user == @profile.user
allow to edit
else
don't allow to edit
答案 1 :(得分:0)
这是我的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
这是配置文件控制器
class ProfilesController < ApplicationController
skip_before_action :authenticate_user!, only: [ :index ]
before_action :find_profiles, only: [:show, :edit, :update, :destroy]
def index
# if params[:id]
# @profiles = Profile.where(handicap: params[:handicap])
# else
@profiles = Profile.all
@hash = Gmaps4rails.build_markers(@profiles) do |profile, marker|
marker.lat profile.latitude
marker.lng profile.longitude
marker.infowindow render_to_string(partial: "/profiles/map_box", locals: { profile: profile })
end
end
end
def show
@profile = Profile.find(params[:id])
end
def new
@profile = Profile.new
end
def create
owner = current_user
@profile = Profile.new(profile_params)
@profile.owner = owner
if @profile.save
redirect_to profiles_path
else
render :new
end
end
def edit
end
def update
if @profile.update(profile_params)
redirect_to profiles_path
else
render :edit
end
end
def destroy
end
private
def profile_params
params.require(:profile).permit(:last_name,:first_name, :address, :search, :handicap, :street, :postbox, :tagline, :skills, :town, :photo)
end
def find_profiles
@profile = Profile.find(params[:id])
end
end