如何解决无路线匹配问题 错误消息 - >没有路由匹配{:action =>“show”,:controller =>“cost”,:format => nil,:id => nil,:travel_id => nil}缺少必需的键:[: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
我的模特:
class Travel < ActiveRecord::Base
belongs_to :user
has_many :costs
end
class Cost < ActiveRecord::Base
belongs_to :travel
end
我的费用_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 %>
如果可能请帮助我,我正处于这个问题的时候。 感谢。
答案 0 :(得分:1)
更改
format.html { redirect_to travel_cost_path(@costs), notice: 'Cost was successfully created.' }
到
format.html { redirect_to travel_cost_path(@travel, @cost), notice: 'Cost was successfully created.' }