我正在尝试在ajax中使用rails嵌套资源,但我收到此错误:ListapromoController处理#create as JS 参数:{" utf8" =>"✓"," detalleprom" => {" Articulo" =>&#34 ; 2"," listaprom_id" =>" 1247"," PromoId" =>" 1247"," Id" =>" 1247"},"提交" =>"创建Detalleprom"," listaprom_id" =>& #34; 1245"} 在1ms内完成400 Bad Request(ActiveRecord:0.0ms)
ActionController :: ParameterMissing(param丢失或值为空:listaprom):
app / controllers / listapromo_controller.rb:81:在listaprom_params'
app/controllers/listapromo_controller.rb:31:in
创建'
这是我的索引视图:
<!--<p id="notice"><%= notice %></p>-->
<h1>Lista de listapromo</h1>
<style>
.container {
}
</style>
<div class="container">
<div class="row">
<div class="text-center">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mynewlistaprom">
Nuevo listaprom
</button>
</div>
</div>
<br>
<br>
<table id="listapromo" class="display"><!--el id listapromo es de datatables referenciado en listapromo.coffe y display class es una clase de datatables-->
<thead>
<tr><!--active es para sombrear la fila-->
<th>ID</th>
<th>Descripción</th>
<th>Caduca</th>
<th>Inicio</th>
<th>Final</th>
<th>Estado</th>
<th>Acción</th>
<th></th>
</tr>
</thead>
<tbody id="container_listapromo">
<%= render @listapromo %><!--carga todos los listapromo-->
</tbody>
</table>
<!-- Modal create action -->
<%= form_for(@listaprom, remote: true, html: {class: "form-horizontal"}) do |f| %> <!--ajax remote: true-->
<div class="modal fade" id="mynewlistaprom" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Agregar listaprom</h4>
</div>
<div class="modal-body">
<div class="form-group">
<%= f.label :Lista, "Clave:", class: "control-label col-md-2" %>
<div class="col-md-5">
<%= f.text_field :Lista, class: "form-control listaprom_clave" %>
</div>
<%= f.label :Caduca,"Caduca:", class: "control-label col-xs-7 col-sm-6 col-md-1"%>
<div class="col-md-1">
<%= f.check_box :Caduca %>
</div>
<%= f.label :Activa,"Inactiva:", class: "control-label col-xs-7 col-sm-6 col-md-1"%>
<div class="col-md-1">
<%= f.check_box :Activa %>
</div>
</div>
<div class="form-group">
<%= f.label :Descripcion,"Descripción:", class: "control-label col-md-2" %>
<div class="col-md-5">
<%= f.text_field :Descripcion, class: "form-control listaprom_descripcion" %>
</div>
<%= f.label :Tipo, "Tipo:", class: "control-label col-md-1" %>
<div class="col-md-3">
<%= f.select :Tipo, ['Producto','Monto de Venta','Volumen'], class: "form-control"%>
</div>
</div>
<div class="well center-block">
<div class="form-group">
<%= f.label :FechaI,"Inicio:", class: "control-label col-md-2" %>
<div class="col-md-3">
<%= f.text_field :FechaI, class: "form-control datepicker listaprom_fechainicio" %>
</div>
<%= f.label :FechaF,"Fin:", class: "control-label col-md-2" %>
<div class="col-md-3">
<%= f.text_field :FechaF, class: "form-control datepicker listaprom_fechafinal" %>
</div>
</div>
</div>
<%= f.hidden_field :IdEmpresa, value: current_usuario.empresa_id %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="mynewlistapromclose">Close</button>
<%= submit_tag "Create", class: "btn btn-primary"%>
</div>
</div>
</div>
</div>
<%end%>
</div>
我的部分_listaprom.html.erb
<tr id="listaprom_<%= listaprom.id %>">
<td><%=listaprom.id%></td>
<td><%=listaprom.Descripcion%></td>
<td><%=listaprom.Caduca%></td>
<td><%=listaprom.FechaI%></td>
<td><%=listaprom.FechaF%></td>
<td><%=listaprom.Activa%></td>
<td>
<button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#myupdatelistaprom_<%= listaprom.id %>">
Edit
</button>
<!--Destroy-->
<%= link_to 'Destroy', listaprom, method: :delete, class: "btn btn-danger btn-xs", remote:true %>
</td>
<td class="no" >
<!--Modal - update listaprom-->
<div nohidden class="modal fade si" id="myupdatelistaprom_<%= listaprom.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Editar listaprom</h4>
</div>
<div class="modal-body">
<%= form_for(listaprom, :method => :put, remote: true, html: {class: "form-horizontal"}) do |f| %><!--ajax-->
<div class="form-group">
<%= f.label :Lista, "Clave:", class: "control-label col-md-2" %>
<div class="col-md-5">
<%= f.text_field :Lista, class: "form-control listaprom_clave" %>
</div>
<%= f.label :Caduca,"Caduca:", class: "control-label col-xs-7 col-sm-6 col-md-1"%>
<div class="col-md-1">
<%= f.check_box :Caduca %>
</div>
<%= f.label :Activa,"Inactiva:", class: "control-label col-xs-7 col-sm-6 col-md-1"%>
<div class="col-md-1">
<%= f.check_box :Activa %>
</div>
</div>
<div class="form-group">
<%= f.label :Descripcion,"Descripción:", class: "control-label col-md-2" %>
<div class="col-md-5">
<%= f.text_field :Descripcion, class: "form-control listaprom_descripcion" %>
</div>
<%= f.label :Tipo, "Tipo:", class: "control-label col-md-1" %>
<div class="col-md-3">
<%= f.select :Tipo, ['Producto','Monto de Venta','Volumen'], class: "form-control"%>
</div>
</div>
<div class="well center-block">
<div class="form-group">
<%= f.label :FechaI,"Inicio:", class: "control-label col-md-2" %>
<div class="col-md-3">
<%= f.text_field :FechaI, class: "form-control datepicker listaprom_fechainicio" %>
</div>
<%= f.label :FechaF,"Fin:", class: "control-label col-md-2" %>
<div class="col-md-3">
<%= f.text_field :FechaF, class: "form-control datepicker listaprom_fechafinal" %>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="myupdatebutton_<%= listaprom.id %>" class="btn btn-default" data-dismiss="modal">Close</button>
<%= submit_tag "Update", class: "btn btn-primary"%>
</div>
<%end%>
<%= form_for([@listaprom,@detalleprom],:url => {:listaprom_id => 1245}, remote: true, html: {class: "form-horizontal"}) do |d| %> <!--ajax remote: true-->
<div class="form-group">
<%= d.label :Articulo, "Articulo:", class: "control-label col-md-2" %>
<div class="col-md-5">
<%= d.text_field :Articulo, class: "form-control listaprom_clave" %>
</div>
<div class="actions">
</div>
<%= d.hidden_field :listaprom_id, value: listaprom.id %>
<%= d.hidden_field :PromoId, value: listaprom.id %>
<%= d.hidden_field :Id, value: listaprom.id %>
<%= d.submit %>
</div>
<%end%>
</div>
</div>
</div>
</div>
</td>
</tr>
我的模型detalleprom.rb
class Detalleprom < ActiveRecord::Base
self.primary_key = 'Id'
belongs_to :listaprom, class_name:"Listaprom", foreign_key: "PromoId"
end
我的模型listaprom.rb
class Listaprom < ActiveRecord::Base
has_many :detallepromo, class_name: "Detalleprom", foreign_key: "PromoId"
end
我的detallepromo控制器
class DetallepromoController < ApplicationController
before_action :set_detalleprom, only: [:show, :edit, :update, :destroy]
before_action :set_listaprom
# GET /detallepromo
# GET /detallepromo.json
def index
@detallepromo = Detalleprom.all
end
# GET /detallepromo/1
# GET /detallepromo/1.json
def show
end
# GET /detallepromo/new
def new
@detalleprom = Detalleprom.new
end
# GET /detallepromo/1/edit
def edit
end
# POST /detallepromo
# POST /detallepromo.json
def create
@detalleprom = Detalleprom.new(detalleprom_params)
@detalleprom.listaprom = @listaprom
respond_to do |format|
if @detalleprom.save
format.html { redirect_to @detalleprom.listaprom, notice: 'Detalleprom was successfully created.' }
format.json { render :show, status: :created, location: @detalleprom }
format.js #ajax
else
format.html { render :new }
format.json { render json: @detalleprom.errors, status: :unprocessable_entity }
format.js #ajax
end
end
end
# PATCH/PUT /detallepromo/1
# PATCH/PUT /detallepromo/1.json
def update
respond_to do |format|
if @detalleprom.update(detalleprom_params)
format.html { redirect_to @detalleprom.listaprom, notice: 'Detalleprom was successfully updated.' }
format.json { render :show, status: :ok, location: @detalleprom }
format.js #ajax
else
format.html { render :edit }
format.json { render json: @detalleprom.errors, status: :unprocessable_entity }
format.js #ajax
end
end
end
# DELETE /detallepromo/1
# DELETE /detallepromo/1.json
def destroy
@detalleprom.destroy
respond_to do |format|
format.html { redirect_to detallepromo_url, notice: 'Detalleprom was successfully destroyed.' }
format.json { head :no_content }
format.js #ajax
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listaprom
@listaprom = Listaprom.find(params[:listaprom_id])
end
def set_detalleprom
@detalleprom = Detalleprom.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def detalleprom_params
params.require(:detalleprom).permit(:Articulo, :listaprom_id, :PromoId, :Cantidad, :Tipo, :TipoProm, :Monto, :Volumen, :TipMed, :IdEmpresa, :Nivel)
end
end
我的listapromo控制器
class ListapromoController < ApplicationController
before_action :set_listaprom, only: [:show, :edit, :update, :destroy]
# GET /listapromo
# GET /listapromo.json
def index
@listapromo = Listaprom.all
@listaprom = Listaprom.new
@detalleprom = Detalleprom.new
end
# GET /listapromo/1
# GET /listapromo/1.json
def show
@detalleprom = Detalleprom.new
end
# GET /listapromo/new
def new
@listaprom = Listaprom.new
end
# GET /listapromo/1/edit
def edit
@detalleprom = Detalleprom.new
end
# POST /listapromo
# POST /listapromo.json
def create
@listaprom = Listaprom.new(listaprom_params)
respond_to do |format|
if @listaprom.save
format.html { redirect_to @listaprom, notice: 'Listaprom was successfully created.' }
format.json { render :show, status: :created, location: @listaprom }
format.js #ajax
else
format.html { render :new }
format.json { render json: @listaprom.errors, status: :unprocessable_entity }
format.js #ajax
end
end
end
# PATCH/PUT /listapromo/1
# PATCH/PUT /listapromo/1.json
def update
respond_to do |format|
if @listaprom.update(listaprom_params)
format.html { redirect_to @listaprom, notice: 'Listaprom was successfully updated.' }
format.json { render :show, status: :ok, location: @listaprom }
format.js #ajax
else
format.html { render :edit }
format.json { render json: @listaprom.errors, status: :unprocessable_entity }
format.js #ajax
end
end
end
# DELETE /listapromo/1
# DELETE /listapromo/1.json
def destroy
@listaprom.destroy
respond_to do |format|
format.html { redirect_to listapromo_url, notice: 'Listaprom was successfully destroyed.' }
format.json { head :no_content }
format.js #ajax
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listaprom
@listaprom = Listaprom.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listaprom_params
params.require(:listaprom).permit(:Lista,:listaprom_id, :Descripcion, :Caduca, :FechaI, :FechaF, :Grupo, :Activa, :Tipo, :IdEmpresa)
end
end
我的路线
Rails.application.routes.draw do
resources :listapromo do
resources :detallepromo
end
答案 0 :(得分:0)
我认为您在尝试提交此表单时遇到错误:
<%= form_for([@listaprom,@detalleprom],:url => {:listaprom_id => 1245}, remote: true, html: {class: "form-horizontal"}) do |d| %> <!--ajax remote: true-->
<div class="form-group">
<%= d.label :Articulo, "Articulo:", class: "control-label col-md-2" %>
<div class="col-md-5">
<%= d.text_field :Articulo, class: "form-control listaprom_clave" %>
</div>
<div class="actions">
</div>
<%= d.hidden_field :listaprom_id, value: listaprom.id %>
<%= d.hidden_field :PromoId, value: listaprom.id %>
<%= d.hidden_field :Id, value: listaprom.id %>
<%= d.submit %>
</div>
<%end%>
如果您尝试更新listaprom
及其关联的detalleprom
,则可以使用field_for
并将accepts_nested_attributes_for
添加到listaprom
模型中。不要忘记修改你的强力params。您在此处执行此操作的方式会向Create Detalleprom
发送ListapromoController
个请求。控制器不理解请求,因此不理解400代码。
以下表单将创建与detalleprom
@listaprom
<%= form_for @listaprom, remote: true do |d| %>
(your code from @listaprom)
<%= d.field_for :detalleprom do |dd| %>
<%= dd.label :Articulo %>
<%= dd.text_field :Articulo %>
(rest of your code for detalleprom)
<% end %>
<%end>