weixin_33694172 2016-09-03 17:04 采纳率: 0%
浏览 25

嵌套资源错误

I am trying to use the rails nested resources in ajax but I get this error: Processing by ListapromoController#create as JS Parameters: {"utf8"=>"✓", "detalleprom"=>{"Articulo"=>"2", "listaprom_id"=>"1247", "PromoId"=>"1247", "Id"=>"1247"}, "commit"=>"Create Detalleprom", "listaprom_id"=>"1245"} Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: listaprom): app/controllers/listapromo_controller.rb:81:in listaprom_params' app/controllers/listapromo_controller.rb:31:increate'

this is my index view:

 <!--<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>

my partial _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>

my model detalleprom.rb

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

my model listaprom.rb

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

my 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

my 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

my routes

Rails.application.routes.draw do

  resources :listapromo do
    resources :detallepromo
  end
  • 写回答

1条回答 默认 最新

  • 零零乙 2016-09-03 19:37
    关注

    I assume you got the error when trying to submit this form:

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

    If you're trying to update listaprom and its associated detalleprom, you can use field_for and add accepts_nested_attributes_for to your listaprom model. Don't forget to modify your strong params too. The way you're doing it here sends a Create Detalleprom request to ListapromoController. The controller doesn't understand the request and thus the 400 code.

    The following form will create a detalleprom associated with @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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致