没有路由匹配“缺少必需的键:[:id,:_id]"; [英] No routes matches "missing required keys: [:id, :_id]"
问题描述
如何解决没有路由匹配错误信息 -> 没有路由匹配 {:action=>"show", :controller=>"cost", :format=>nil, :id=>nil, :travel_id=>nil} 缺失必需的键:[:id, :travel_id]
<小时>Routes.rb:
资源:旅行做资源:成本结尾资源:简介资源:家园设计为:用户
<小时>
Costs_controller:
before_action :set_travelbefore_action :set_cost, only: [:show, :edit, :update, :destroy]定义索引@travel = Travel.find(params[:travel_id])如果 current_user.present?@costs = @travel.costs.all别的redirect_to '/users/sign_in'结尾结尾定义新@travel = Travel.find(params[:travel_id])如果 current_user.present?@cost = @travel.costs.new别的redirect_to '/users/sign_in'结尾结尾定义创建@travel = Travel.find(params[:travel_id])@cost = @travel.costs.new(costs_params)response_to do |格式|如果@cost.saveformat.html { redirect_to travel_cost_path(@costs),注意:'成本已成功创建.'}format.json { 渲染:显示,状态::创建,位置:@cost }别的format.html { 渲染:新}format.json { 渲染 json: @cost.errors, 状态: :unprocessable_entity }结尾结尾结尾
<小时>
我的模型:
class Travel
<小时>
我的费用_form:
<%= form_for [@travel, @cost] do |f|%><% 如果@cost.errors.any?%><div id="error_explanation"><h2><%=pluralize(@cost.errors.count, "error") %>禁止节省这笔费用:</h2><ul><% @cost.errors.full_messages.each do |message|%><li><%=消息%></li><%结束%>
<%结束%><div class="field"><%= f.label :value %><br><%= f.text_field :value %>
<div class="field"><%= f.label :dat %><br><%= f.date_select :dat %>
<div class="field"><%= f.label :cost_type %><br><%= f.select :cost_type, [['Feeds', 'Feeds'],['燃料','燃料'],['停车', '停车'],['收费','收费'],['住宿', '住宿'],['其他','其他']]%>