带有rails

时间:2016-09-02 19:02:07

标签: ruby-on-rails ruby-on-rails-4

我尝试以单一模态形式使用嵌套资源,我收到此错误:“无路由匹配{:action =>”index“,:controller =>”detallepromo“,:listaprom_id => nil}缺少必需的键:[:listaprom_id]“如何在没有idparam的情况下使用嵌套表单?我正在使用本教程在索引视图中执行添加和编辑操作:https://www.youtube.com/watch?v=2Il7PPhen3o

我的索引视图:

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


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

我的部分视图(在此表单中编辑我想放置嵌套资源)

<tr id="listaprom_<%= listaprom.id %>">
  <td><%=listaprom.id%></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-->
  <%= form_for(listaprom, :method => :put, remote: true, html: {class: "form-horizontal"}) do |f| %><!--ajax-->
    <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">

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


            </div>

            <%= form_for([@listaprom,@detalleprom],  remote: true, html: {class: "form-horizontal"}) do |f| %> <!--ajax remote: true-->

              <div class="form-group">
                <%= f.label :Articulo, "Articulo:", class: "control-label col-md-2" %>
                <div class="col-md-5">
                  <%= f.text_field :Articulo, class: "form-control listaprom_clave" %>
                </div>
                <%= f.hidden_field :listaprom_id, value:"1341" %>
                <%= f.hidden_field :PromoId, value:"1341" %>

              </div>

            <%end%>

          </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>
        </div>
      </div>
    </div>
  <%end%></td>
</tr>

我的路线

  resources :listapromo do
    resources :detallepromo
  end

我的模特

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

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

我的控制器

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
  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, :Descripcion, :Caduca, :FechaI, :FechaF, :Grupo, :Activa, :Tipo, :IdEmpresa)
    end
end

1 个答案:

答案 0 :(得分:0)

抱歉无法发表评论,因为我没有足够的代表。

尝试在强参数中允许:listaprom_id

还可以显示实际的完整错误和detalleprom控制器代码。

从我能理解的情况来看,问题在于,:listaprom_id无法通过detallepromo#Index1,或者由于某种原因而未接受:listaprom_id

同样在某些地方Detalleprom和某些地方detallepromo。你能解释一下吗?