Rails 6: Only one profile per user should be created

☆樱花仙子☆ 提交于 2019-12-19 12:23:58

问题


I'm currently working on a Rails 6 application. I have the following association. A User has a Profile and a Profile belongs to a User. When editing a profile for a user I ended up having two profiles for the user. I would like to have only one profile per user.

Edit form: 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 %>

I see from the console that user 1 has 2 profiles. I'm not sure how a profile was created maybe I hit the create method from profile controller but mistake but I would like for this not to happen. Is there a validation for only one profile to belong to user?

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

I'm not sure if the issue comes from the way the routes are configured?

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

From the below snippet you can see that a user has 2 profiles for 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">]

Not sure where the issue came from.


回答1:


I once solved this by doing (I know it's strange, I'm just putting it out here as an idea)

has_one  :profile
has_many :profiles

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

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




回答2:


I decided to not to use a profile controller and just have one model User that can be used as the profile.



来源:https://stackoverflow.com/questions/56621887/rails-6-only-one-profile-per-user-should-be-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!