gpt4 book ai didi

ruby-on-rails - 错误 "param is missing or the value is empty: personas_x_tipos_persona"

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:25 26 4
gpt4 key购买 nike

我从 Rails 开始,我遇到了这个我无法解决的错误。

Error - param is missing or the value is empty: personas_x_tipos_persona

Controller

class PersonasController < ApplicationController
def create_cliente
@cliente = Persona.new(persona_params)
@personas_x_tipos_personas = Persona.new(tipos_personas_params)
if @cliente.save
redirect_to show_clientes_path
else
render :new_cliente
end
end
private
def persona_params
params.require(:persona).permit(:nombre, :apellido, :direccion, :ruc, :contacto, :email)
end
def tipos_personas_params
params.require(:personas_x_tipos_persona).permit(:linea_credito)
end
end

查看

<div>
<%= form_for :persona ,:url => add_cliente_path, :html => {:method => :post} do |f|%>
<% @cliente.errors.full_messages.each do |message| %>
<div class="alert alert-danger" margin-top:10px">
* <%=message%>
</div>
<% end %>

<%= f.text_field :nombre, placeholder: "Nombre del Cliente"%>
<%= f.text_field :apellido, placeholder: "Apellido del Cliente"%>
<%= f.text_field :direccion, placeholder: "Direccion del Cliente"%>
<%= f.text_field :ruc, placeholder: "RUC del Cliente"%>
<%= f.text_field :contacto, placeholder: "Contacto del Cliente"%>
<%= f.email_field :email, placeholder: "Email del Cliente""%>

<%= f.fields_for :personas_x_tipos_persona do |pxp|%>
<%= pxp.number_field :linea_credito, placeholder: "Linea de Credito del Cliente"%>
<% end %>
<%= f.submit 'Guardar'%>
<% end %>
</div>

最佳答案

param is missing or the value is empty: personas_x_tipos_persona

问题在于 @personas_x_tipos_personas = Persona.new(tipos_personas_params)(实际上不需要)这行正在调用 tipos_personas_params .

来自 require(key) 的文档,

When passed a single key, if it exists and its associated value is either present or the singleton false, returns said value

Otherwise raises ActionController::ParameterMissing

因此,在您的情况下,require 需要 :personas_x_tipos_persona,而 params 中缺少它,因此错误也是如此。

实际上,表单对象:persona 而不是:personas_x_tipos_persona。另外我可以看到你正在使用 fields_for,所以你需要在 persona_paramstipos_personas_params 中加入白名单 :personas_x_tipos_persona_attributes > 不需要方法。下面的代码应该可以让你继续。

class PersonasController < ApplicationController
def create_cliente
@cliente = Persona.new(persona_params)
#this is not needed
#@personas_x_tipos_personas = Persona.new(tipos_personas_params)
if @cliente.save
redirect_to show_clientes_path
else
render :new_cliente
end
end

private
def persona_params
params.require(:persona).permit(:nombre, :apellido, :direccion, :ruc, :contacto, :email, personas_x_tipos_persona_attributes: [:id, :linea_credito])
end
end

关于ruby-on-rails - 错误 "param is missing or the value is empty: personas_x_tipos_persona",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639138/

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