Rails 6:每个用户只能创建一个配置文件 [英] Rails 6: Only one profile per user should be created

查看:51
本文介绍了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 class="form-group"><%= f.label :full_name, '全名' %><%= f.text_field :full_name, autofocus: true, class: "form-control" %>

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

<div class="form-group"><%= f.label :bio, 'Bio'%><p>你为什么加入ArtsySpace?这里的其他人应该了解你什么?</p><%= f.text_field :bio, class: "form-control"%>

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

<%结束%>

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

class ProfilesController <应用控制器定义新@profile = current_user.build_profile结尾定义创建@profile = current_user.create_profile(profile_params)@profile.avatar.attach(params[:profile][:avatar])如果@profile.save重定向到@post别的呈现新"结尾结尾高清秀@profile = Profile.find(params[:id])结尾定义编辑@profile = current_user.profile结尾定义更新@profile = current_user.profile如果@profile.update!(profile_params)redirect_to @profile,注意:'配置文件已成功更新.'别的渲染:编辑结尾结尾定义删除@profile = current_user.profile.find(params[:id])@profile.destroy结尾私人的def profile_paramsparams.require(:profile).permit(:full_name, :city, :bio, :avatar)结尾结尾

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

Rails.application.routes.draw 做设计为:用户devise_scope : 用户做资源 :profiles, only: [:edit, :update]结尾资源 :profiles, only: [:show]资源:帖子做资源:评论,仅:%i[显示新创建编辑更新]结尾结尾类用户<申请记录# 包括默认设计模块.其他可用的有:# :confirmable, :lockable, :timeoutable, :trackable 和 :omniauthable设计 :database_authenticable, :registerable,:recoverable, :rememberable, :validatablehas_many : 帖子has_one :个人资料accepts_nested_attributes_for :profile结尾

从下面的代码片段中,您可以看到用户有 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">, #]

不确定问题出在哪里.

解决方案

我曾经通过这样做解决了这个问题(我知道这很奇怪,我只是把它作为一个想法放在这里)

has_one :profilehas_many :个人资料验证 :profiles, 长度: { in: 0..1 }

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

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.

解决方案

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

这篇关于Rails 6:每个用户只能创建一个配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆