【问题标题】:nested resources with rails带有rails的嵌套资源
【发布时间】:2017-01-10 23:15:44
【问题描述】:

我尝试以单一模式形式使用嵌套资源,但出现此错误:“No route matches {:action=>"index", :controller=>"detallepromo", :listaprom_id=>nil} missing必需的键:[: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

【问题讨论】:

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


    【解决方案1】:

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

    尝试在您的强参数中允许:listaprom_id

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

    据我所知,问题是你没有通过:listaprom_id 每当它去detallepromo#Index1 或者它不接受:listaprom_id 出于某种原因。

    在某些地方它是Detalleprom,在某些地方它是detallepromo。能解释一下吗?

    【讨论】:

    • 我粘贴了它,但抛出了同样的错误,因为 routhes 应该是这样的:localhost:3000/listapromo/1/edit 在 url 中传递 id 但我不使用这种方式,因为我正在工作ajax 在索引视图中,当我重新加载页面时出现错误。在这一行: 应该是在同一页面中传递 id 的一种方式
    • 可以通过添加:url =&gt; {:listaprom_id =&gt; @listaprom.id}传递id
    • 我粘贴了它,但现在我可能会因为@listaprom ActionController::ParameterMissing(参数丢失或值为空:listaprom)收到此错误:app/controllers/listapromo_controller.rb:81:in listaprom_params' app/controllers/listapromo_controller.rb:31:in create'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多