2014-07-22 1 views
0

해결 방법 일치하는 경로가 없습니다. 오류 메시지 -> 경로가 일치하지 않습니다 {: action => "show", : controller => " 선정 된 ": 형식 => 무 : ID => 무 : travel_id => 닐} 필요한 키 누락 : [: ID : travel_id]"필수 키가 누락되었습니다 : [: id, : _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

에? – Pavan

+0

Cost_controller format.html {redirect_to travel_cost_path (@costs), 통지 : '비용을 성공적으로 생성했습니다.' } –

답변

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.' } 
+0

예! 고마워 –

관련 문제