没有路由匹配“缺少必需的键:[:id,:_id]"; [英] No routes matches "missing required keys: [:id, :_id]"

查看:38
本文介绍了没有路由匹配“缺少必需的键:[: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'],['燃料','燃料'],['停车', '停车'],['收费','收费'],['住宿', '住宿'],['其他','其他']]%>

<div 类 =字段"><%= f.label :description %><br><%= f.text_field :description %>

<div class="actions"><%= f.submit %>

<%结束%>

如果可能,请帮助我,我正遇到这个问题.谢谢.

解决方案

更改

format.html { redirect_to travel_cost_path(@costs),注意:'成本创建成功.'}

format.html { redirect_to travel_cost_path(@travel, @cost),注意:'成本创建成功.'}

How do I solve No routes matches Error message -> No route matches {:action=>"show", :controller=>"cost", :format=>nil, :id=>nil, :travel_id=>nil} missing required keys: [:id, :travel_id]


Routes.rb:

 resources :travels do
    resources :costs
  end

  resources :profiles

  resources :homes

  devise_for :users


Costs_controller:

  before_action :set_travel
  before_action :set_cost, only: [:show, :edit, :update, :destroy]

    def index
        @travel = Travel.find(params[:travel_id]) 
        if current_user.present?
          @costs = @travel.costs.all
        else
          redirect_to '/users/sign_in'
        end

      end

def new
    @travel = Travel.find(params[:travel_id])
    if current_user.present?
      @cost = @travel.costs.new 
    else
      redirect_to '/users/sign_in'
    end 
  end

def create
    @travel = Travel.find(params[:travel_id])
    @cost = @travel.costs.new(costs_params)
    respond_to do |format|
      if @cost.save
        format.html { redirect_to travel_cost_path(@costs), notice: 'Cost was successfully created.' }
        format.json { render :show, status: :created, location: @cost }
      else
        format.html { render :new }
        format.json { render json: @cost.errors, status: :unprocessable_entity }
      end
    end
  end


My model:

class Travel < ActiveRecord::Base
    belongs_to :user
    has_many :costs
end

class Cost < ActiveRecord::Base
    belongs_to :travel  
end


My cost _form:

<%= form_for [@travel, @cost] do |f| %>
  <% if @cost.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@cost.errors.count, "error") %> prohibited this cost from being saved:</h2>

      <ul>
      <% @cost.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :value %><br>
    <%= f.text_field :value %>
  </div>
  <div class="field">
    <%= f.label :dat %><br>
    <%= f.date_select :dat %>
  </div>
  <div class="field">
    <%= f.label :cost_type %><br>

    <%= f.select :cost_type, [['Feeds', 'Feeds'],
                         ['Fuel', 'Fuel'],
                         ['Parking', 'Parking'],
                         ['Toll', 'Toll'],
                         ['Accomodation', 'Accomodation'],
                         ['Other', 'Other']]
     %>  
  </div>

  <div class ="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>


  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Help me if possible, I'm at a time with this problem. Thanks.

解决方案

Change

format.html { redirect_to travel_cost_path(@costs), notice: 'Cost was successfully created.' }

to

format.html { redirect_to travel_cost_path(@travel, @cost), notice: 'Cost was successfully created.' }

这篇关于没有路由匹配“缺少必需的键:[:id,:_id]";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆