gpt4 book ai didi

ruby-on-rails - Ruby on Rails - 关联错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:51 25 4
gpt4 key购买 nike

我在我的 RoR 项目中遇到了关联问题;我解释:

我有两个表:

  • Admin - 使用 Devise 创建。
  • 联系人 - 使用 rails 标准控制台命令 rails g scaffold 创建。

Admin 表与 Contact 表具有 1:n 关联,因此在它们各自的类中:

管理员:

class Admin < ActiveRecord::Base
has_many :contacts

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

end

联系方式

class Contact < ActiveRecord::Base
belongs_to :admin
validates :nome, :cognome, :indirizzo_abitazione,
:numero_civico, :indirizzo_email, :prefisso_cellulare, :cellulare, presence: true
validates :indirizzo_email, :indirizzo_email_2, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
end

当我创建一个联系人时,我需要知道的第一件事是创建它的管理员的 ID,所以我修改了 contacts_controller 中的“创建”方法

    def create
@admin = Admin.find_by id: current_admin.id

@contact = @admin.contacts.build(:admin_id => @admin.id)

respond_to do |format|
if @contact.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end

end

当我尝试创建联系人(提交按钮操作)时,在我填写完所有字段后,尽管我的所有字段都有效,但我收到了很多验证错误。所以我认为在创建方法期间没有传递字段的参数。

我检查了 contact_params 方法,但所有字段都包括在内:

def contact_params
params.require(:contact).permit(:nome, :cognome, :indirizzo_abitazione, :numero_civico, :indirizzo_email, :indirizzo_email_2, :prefisso_cellulare, :cellulare, :telefono_casa)
end

我哪里错了?

我还在这里添加了 _form.html.erb 代码

  <%= form_for(@contact) do |f| %>
<% if @contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>

<ul>
<% @contact.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group <%= @contact.errors[:nome].present? == true ? "has-error" : "" %>" >
<%= f.label :nome , :class => "control-label" %><br>
<%= f.text_field :nome, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:cognome].present? ? "has-error" : "" %>">
<%= f.label :cognome, :class => "control-label" %><br>
<%= f.text_field :cognome, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:indirizzo_abitazione].present? ? "has-error" : "" %>">
<%= f.label :indirizzo_abitazione, :class => "control-label" %><br>
<%= f.text_field :indirizzo_abitazione, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:numero_civico].present? ? "has-error" : "" %>">
<%= f.label :numero_civico, :class => "control-label" %><br>
<%= f.text_field :numero_civico, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:indirizzo_email].present? ? "has-error" : "" %>">
<%= f.label :indirizzo_email, :class => "control-label" %><br>
<%= f.text_field :indirizzo_email, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:indirizzo_email_2].present? ? "has-error" : "" %>">
<%= f.label :indirizzo_email_2, :class => "control-label" %><br>
<%= f.text_field :indirizzo_email_2, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:prefisso_cellulare].present? ? "has-error" : "" %>">
<%= f.label :prefisso_cellulare, :class => "control-label" %><br>
<%= f.text_field :prefisso_cellulare, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:cellulare].present? ? "has-error" : "" %>">
<%= f.label :cellulare, :class => "control-label" %><br>
<%= f.text_field :cellulare, :class => "form-control" %>
</div>
<div class="form-group <%= @contact.errors[:telefono_casa].present? ? "has-error" : "" %>">
<%= f.label :telefono_casa, :class => "control-label" %><br>
<%= f.text_field :telefono_casa, :class => "form-control" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

最佳答案

在我看来,您的问题出在您构建新联系人时的 create 方法中:

@contact = @admin.contacts.build(:admin_id => @admin.id)

上面的代码不包含您的contact_params,因此不会通过验证。您只是与 admin_id 建立联系。您的代码必须如下所示才能通过验证:

@contact = @admin.contacts.build(contact_params)

另请注意,您不需要在 build 中包含 :admin_id => @admin.id,因为您是通过 @ 创建联系人的管理。 Rails 足够聪明,知道将新联系人与 @admin 相关联!

关于ruby-on-rails - Ruby on Rails - 关联错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31164323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com