ネストされたリソースを単一のモーダルフォームで使用しようとしましたが、次のエラーが表示されます: "ルートが一致しません{: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">×</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を渡しますが、インデックスビューでwhith ajaxを使用しているため、ページをリロードするとエラーが表示されるためです。この行では、<%= form_for([@ listaprom、@ detalleprom]、remote:true、html:{class: "form-horizontal"}))do | f | %:>同じページにidを渡す方法にする必要があります – LuisC
':url => {:listaprom_id => @ listaprom.id}' – Ravi
を貼り付けてIDを渡すことができますが、今はこのエラーmayby @ listaprom ActionController :: ParameterMissing(paramがないか値が空です:listaprom): app/controllers/listapromo_controller.rb:81:listaprom_paramsの中で app/controllers/listapromo_controller.rb:31: 'create ' – LuisC