【问题标题】:Create a 1:1 relation between User and a Profile在用户和个人资料之间创建 1:1 关系
【发布时间】:2016-05-12 09:51:01
【问题描述】:

我对 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

我不知道为什么,但是今天,一个用户可以创建许多个人资料并编辑另一个个人资料。

有人能帮我理解为什么吗?

【问题讨论】:

  • 我认为问题不在于模型,而在于控制器,给我们看更多代码
  • 是的,请向我们展示控制器以及 DB 文件夹中的 schema.rb
  • 参见下面的架构和配置文件控制器
  • @Fred 你检查了我下面的代码吗?我还是卡住了! :(
  • @RuNpiXelruN 有什么想法吗?

标签: ruby-on-rails activerecord devise user-profile


【解决方案1】:

为了防止用户编辑其他个人资料,您可以在个人资料控制器更新操作中执行类似的操作

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

【讨论】:

  • !感谢您的回复 !但是你能看看我的代码吗?我还有铅
【解决方案2】:

这是我的 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

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 1970-01-01
    • 2018-08-02
    • 2018-03-26
    • 2015-01-29
    • 2019-12-16
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多