2016-09-03 2 views
0

Ich versuche, die verschachtelten Ressourcen rails in Ajax zu verwenden, aber ich bekomme diesen Fehler: Verarbeitung von ListapromoController # als JS erstellen Parameter: {"utf8" => "✓", "detalleprom" => { "Articulo" => "2", "listaprom_id" => "1247", "PromoId" => "1247", "Id" => "1247"}, "commit" => "Detalleprom erstellen", "listaprom_id" => "1245"} 400 Bad Request in 1ms Completed (Active: 0,0 ms)Fehler mit verschachtelten Ressourcen

Action :: ParameterMissing (param fehlt oder der Wert leer ist: listaprom): app/controllers/listapromo_controller.rb: 81 : in listaprom_params' app/controllers/listapromo_controller.rb:31:in erstellen '

das ist meine Indexansicht:

<!--<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">&times;</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> 

meine Teil _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">&times;</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> 

mein Modell detalleprom.rb

class Detalleprom < ActiveRecord::Base 
    self.primary_key = 'Id' 
    belongs_to :listaprom, class_name:"Listaprom", foreign_key: "PromoId" 
end 

mein Modell listaprom.rb

class Listaprom < ActiveRecord::Base 
    has_many :detallepromo, class_name: "Detalleprom", foreign_key: "PromoId" 
end 

mein detallepromo Controller

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 

mein listapromo Controller

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 

meine Routen

Rails.application.routes.draw do 

    resources :listapromo do 
    resources :detallepromo 
    end 
+0

schaffen klingt wie Sie die Variable durch Einheimische passieren müssen: {} zu '_listaprom.html.erb' ... sehen diese http: // api.rubyonrails.org/classes/ActionView/PartialRenderer.html –

+0

danke für die Antwort @Timmy Ich habe versucht, aber es ist das gleiche Ergebnis: Einheimische: {listaprom_id: listapromo.ids}%> – LuisC

Antwort

0

Ich nehme an, Sie den Fehler bekam, wenn dieses Formular abschicken versuchen:

<%= 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%> 

Wenn Sie versuchen, zu aktualisieren listaprom und seine zugehörigen detalleprom, können Sie field_for verwenden und accepts_nested_attributes_for zu Ihrem listaprom Modell hinzufügen. Vergessen Sie nicht, auch Ihre starken Parameter zu ändern. Die Art, wie Sie es hier tun, sendet eine Create Detalleprom Anfrage an ListapromoController. Der Controller versteht die Anforderung und somit den 400-Code nicht.

Das folgende Formular ein detalleprom im Zusammenhang mit @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> 
+0

Danke für die Antwort, ich versuche, eine Detalleprom zu erstellen aber dieses Formular ist außerhalb des Editierformulars von listaprom. Ich muss ein Detalleprom erstellen, weil listaprom viele Detalleprom hat und ich zwei oder mehr in einem vorhandenen Listaprom hinzufügen muss, das eine Liste erstellt. – LuisC

+0

So wie ich es verstehe, möchten Sie einen Datelleprom erstellen, der zu einem bestimmten Listaprom gehört. Das ist die genaue Art von Situation, in der Sie field_for verwenden sollten. – EJ2015

+0

gerade meine Antwort bearbeitet, um ein Beispiel zu zeigen – EJ2015

Verwandte Themen