Rails 6:每个用户只能创建一个配置文件

时间:2019-06-16 19:16:02

标签: ruby-on-rails ruby ruby-on-rails-6

我目前正在使用Rails 6应用程序。我有以下关联。用户具有个人资料,个人资料属于用户。在为用户编辑配置文件时,我最终得到了该用户的两个配置文件。我希望每个用户只有一个个人资料。

编辑表单:profile / edit.html.erb

<%= form_for @profile do |f| %>
        <div class="form-group">
          <%= f.label :avatar %>
          <%= f.file_field :avatar, as: :file, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :full_name, 'Full Name' %>
          <%= f.text_field :full_name, autofocus: true, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :city, 'City' %>
          <%= f.text_field :city, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :bio, 'Bio'%>
            <p> Why did you join ArtsySpace?
            What should other people here know about you?
            </p>
          <%= f.text_field :bio, class: "form-control"%>
        </div>

        <div class="form-group">
          <%= f.submit "Edit profile", class: "btn btn-primary" %>
        </div>
      <% end %>

我从控制台看到用户1有2个配置文件。我不确定配置文件是如何创建的,也许我从配置文件控制器中单击了create方法,但出错了,但我希望这种情况不会发生。是否只有一个配置文件属于用户的验证?

class ProfilesController < ApplicationController
  def new
    @profile = current_user.build_profile
  end

  def create
    @profile = current_user.create_profile(profile_params)
    @profile.avatar.attach(params[:profile][:avatar])

    if @profile.save
      redirect_to @post
    else
      render 'new'
    end
  end

  def show
    @profile = Profile.find(params[:id])
  end

  def edit
    @profile = current_user.profile
  end

  def update
    @profile = current_user.profile
      if @profile.update!(profile_params)
        redirect_to @profile, notice: 'Profile was successfully updated.'
      else
        render :edit
      end
  end

  def delete
    @profile = current_user.profile.find(params[:id])
    @profile.destroy
  end

  private

  def profile_params
    params.require(:profile).permit(:full_name, :city, :bio, :avatar)
  end
end

我不确定问题是否来自路由的配置方式?

Rails.application.routes.draw do
  devise_for :users

  devise_scope :users do
    resources :profiles, only: [:edit, :update]
  end

  resources :profiles, only: [:show]

  resources :posts do
    resource :comments, only: %i[show new create edit update]
  end
end

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :posts
  has_one :profile
  accepts_nested_attributes_for :profile
end

从下面的代码片段中,您可以看到用户具有2个用于user_id的配置文件:1

[#<Profile id: 3, user_id: 1, full_name: "steven ", city: "diego ", bio: "Because im ", created_at: "2019-06-12 23:11:49", updated_at: "2019-06-16 18:49:22">, #<Profile id: 4, user_id: 1, full_name: "andrew", city: "Tony", bio: "because i know ", created_at: "2019-06-12 23:12:35", updated_at: "2019-06-16 18:51:22">]

不确定问题的出处。

2 个答案:

答案 0 :(得分:0)

我曾经通过解决此问题(我知道这很奇怪,我只是将它作为一种想法在这里发布)

has_one  :profile
has_many :profiles

validates :profiles, length: { in: 0..1 }

https://guides.rubyonrails.org/active_record_validations.html#length

答案 1 :(得分:-1)

我决定不使用profile控制器,而只有一个模型User可以用作配置文件。