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.