0
我尝试使用单一模式形式的嵌套资源,我得到这个错误:“没有路由匹配{:行动=>”索引“,:控制器=>”detallepromo“,:listaprom_id = >无}缺少必需的键:[: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">×</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">×</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>
我routhes
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
我粘贴它,但引发了同样的错误,因为这些线程应该像这样:http:// localhost:3000/listapromo/1 /编辑在URL中传递id但我不使用这种方式因为我在索引视图中使用了ajax,并且在重新加载页面时出现错误。在这一行中:<%= form_for([@ listaprom,@ detalleprom],remote:true,html:{class:“form-horizontal”})do | f | %>应该是在同一页面传递id的方式 – LuisC
你可以通过添加':url => {:listaprom_id => @ listaprom.id}来传递id' – Ravi
我粘贴了它,但现在我得到这个错误mayby #listaprom_params' app/controllers/listapromo_controller.rb:31:在'create中“ – LuisC