ActionController::RoutingError (No route matches [POST] “/cars/1/consumptions/new”):












0















I have this routing error when I add a consumption (after submit it fails) and I am stuck, What am I doing wrong?



A user can have several cars and for each of his car he wants to look after his gas consumptions.



I have three active record models



create_table "cars", force: :cascade do |t|
t.string "car_name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumption_searches", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumptions", force: :cascade do |t|
t.float "total_price"
t.float "kilometers"
t.string "shop"
t.float "liter_price"
t.float "total_liters"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "car_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end



  • car belongs_to :users and has_many :consumptions

  • user has_many :cars, and has_many :consumptions through: :cars

  • consumption belong_to :car and belongs_to :user


My create method in consumptions_controller.rb



def create 
@car = Car.find(params[:car_id])
@consumption = Consumptions.new(consumption_params)
@consumption.car = @car
if @consumption.save!
redirect_to car_consumptions_path, notice: 'consumption was successfully created.'
else
render :new
end
end


cars_controller.rb



def show
@car = Car.find(params[:id])
@search = ConsumptionSearch.new(params[:search])
@consumptions = @search.date_range
@consumptions = @consumptions.order('created_at ASC').where(car_id: @car.id)
end


views/consumptions/_form.html.erb



<%= simple_form_for car_consumptions_path do |f| %>
...
<% end %>


routes.rb



Rails.application.routes.draw do
devise_for :users
root to: "cars#index"

resources :cars do
resources :consumptions
end
end


rails routes | grep consumption



  car_consumptions GET    /cars/:car_id/consumptions(.:format)                                                     consumptions#index
POST /cars/:car_id/consumptions(.:format) consumptions#create
new_car_consumption GET /cars/:car_id/consumptions/new(.:format) consumptions#new
edit_car_consumption GET /cars/:car_id/consumptions/:id/edit(.:format) consumptions#edit
car_consumption GET /cars/:car_id/consumptions/:id(.:format) consumptions#show
PATCH /cars/:car_id/consumptions/:id(.:format) consumptions#update
PUT /cars/:car_id/consumptions/:id(.:format) consumptions#update
DELETE /cars/:car_id/consumptions/:id(.:format) consumptions#destroy


As requested
enter image description here



EDIT



here is what I have in the HTML if it can help:



<form novalidate="novalidate" class="simple_form /cars/1/consumptions" action="/cars/1/consumptions/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="0nwq/pQSXCU2ptBjbewTCBffPLpZZUPAj6/HPQTGtYd8cHz9zv8R/C/JYnXPDpKw5o3/vGlVtav2Sa2nSvgOQdQ==">









share|improve this question

























  • What's the exact line that shows the error (show the full stacktrace)? because the error points to a "new" action but you are not using new_car_consumption anywhere. The error says that, somewhere, you are trying to point to new_car_consumption using method POST which is not right, it should be GET.

    – arieljuod
    Nov 20 '18 at 17:11













  • updated, it's after submiting the new consumption

    – johan
    Nov 20 '18 at 17:16











  • car_consumptions_path is missing a car_id on both places you use it, also simple_form_for expect the first parameter to be an object and you are passing a helper, it should be something like: simple_form_for [@consumption, @car] for nested resources. You are still not showing the full error stacktrace (at least file name and line number)

    – arieljuod
    Nov 20 '18 at 17:27











  • The picture of the error page is not showing the stacktrace of the error. Click on Full Trace

    – arieljuod
    Nov 20 '18 at 18:51
















0















I have this routing error when I add a consumption (after submit it fails) and I am stuck, What am I doing wrong?



A user can have several cars and for each of his car he wants to look after his gas consumptions.



I have three active record models



create_table "cars", force: :cascade do |t|
t.string "car_name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumption_searches", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumptions", force: :cascade do |t|
t.float "total_price"
t.float "kilometers"
t.string "shop"
t.float "liter_price"
t.float "total_liters"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "car_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end



  • car belongs_to :users and has_many :consumptions

  • user has_many :cars, and has_many :consumptions through: :cars

  • consumption belong_to :car and belongs_to :user


My create method in consumptions_controller.rb



def create 
@car = Car.find(params[:car_id])
@consumption = Consumptions.new(consumption_params)
@consumption.car = @car
if @consumption.save!
redirect_to car_consumptions_path, notice: 'consumption was successfully created.'
else
render :new
end
end


cars_controller.rb



def show
@car = Car.find(params[:id])
@search = ConsumptionSearch.new(params[:search])
@consumptions = @search.date_range
@consumptions = @consumptions.order('created_at ASC').where(car_id: @car.id)
end


views/consumptions/_form.html.erb



<%= simple_form_for car_consumptions_path do |f| %>
...
<% end %>


routes.rb



Rails.application.routes.draw do
devise_for :users
root to: "cars#index"

resources :cars do
resources :consumptions
end
end


rails routes | grep consumption



  car_consumptions GET    /cars/:car_id/consumptions(.:format)                                                     consumptions#index
POST /cars/:car_id/consumptions(.:format) consumptions#create
new_car_consumption GET /cars/:car_id/consumptions/new(.:format) consumptions#new
edit_car_consumption GET /cars/:car_id/consumptions/:id/edit(.:format) consumptions#edit
car_consumption GET /cars/:car_id/consumptions/:id(.:format) consumptions#show
PATCH /cars/:car_id/consumptions/:id(.:format) consumptions#update
PUT /cars/:car_id/consumptions/:id(.:format) consumptions#update
DELETE /cars/:car_id/consumptions/:id(.:format) consumptions#destroy


As requested
enter image description here



EDIT



here is what I have in the HTML if it can help:



<form novalidate="novalidate" class="simple_form /cars/1/consumptions" action="/cars/1/consumptions/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="0nwq/pQSXCU2ptBjbewTCBffPLpZZUPAj6/HPQTGtYd8cHz9zv8R/C/JYnXPDpKw5o3/vGlVtav2Sa2nSvgOQdQ==">









share|improve this question

























  • What's the exact line that shows the error (show the full stacktrace)? because the error points to a "new" action but you are not using new_car_consumption anywhere. The error says that, somewhere, you are trying to point to new_car_consumption using method POST which is not right, it should be GET.

    – arieljuod
    Nov 20 '18 at 17:11













  • updated, it's after submiting the new consumption

    – johan
    Nov 20 '18 at 17:16











  • car_consumptions_path is missing a car_id on both places you use it, also simple_form_for expect the first parameter to be an object and you are passing a helper, it should be something like: simple_form_for [@consumption, @car] for nested resources. You are still not showing the full error stacktrace (at least file name and line number)

    – arieljuod
    Nov 20 '18 at 17:27











  • The picture of the error page is not showing the stacktrace of the error. Click on Full Trace

    – arieljuod
    Nov 20 '18 at 18:51














0












0








0








I have this routing error when I add a consumption (after submit it fails) and I am stuck, What am I doing wrong?



A user can have several cars and for each of his car he wants to look after his gas consumptions.



I have three active record models



create_table "cars", force: :cascade do |t|
t.string "car_name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumption_searches", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumptions", force: :cascade do |t|
t.float "total_price"
t.float "kilometers"
t.string "shop"
t.float "liter_price"
t.float "total_liters"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "car_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end



  • car belongs_to :users and has_many :consumptions

  • user has_many :cars, and has_many :consumptions through: :cars

  • consumption belong_to :car and belongs_to :user


My create method in consumptions_controller.rb



def create 
@car = Car.find(params[:car_id])
@consumption = Consumptions.new(consumption_params)
@consumption.car = @car
if @consumption.save!
redirect_to car_consumptions_path, notice: 'consumption was successfully created.'
else
render :new
end
end


cars_controller.rb



def show
@car = Car.find(params[:id])
@search = ConsumptionSearch.new(params[:search])
@consumptions = @search.date_range
@consumptions = @consumptions.order('created_at ASC').where(car_id: @car.id)
end


views/consumptions/_form.html.erb



<%= simple_form_for car_consumptions_path do |f| %>
...
<% end %>


routes.rb



Rails.application.routes.draw do
devise_for :users
root to: "cars#index"

resources :cars do
resources :consumptions
end
end


rails routes | grep consumption



  car_consumptions GET    /cars/:car_id/consumptions(.:format)                                                     consumptions#index
POST /cars/:car_id/consumptions(.:format) consumptions#create
new_car_consumption GET /cars/:car_id/consumptions/new(.:format) consumptions#new
edit_car_consumption GET /cars/:car_id/consumptions/:id/edit(.:format) consumptions#edit
car_consumption GET /cars/:car_id/consumptions/:id(.:format) consumptions#show
PATCH /cars/:car_id/consumptions/:id(.:format) consumptions#update
PUT /cars/:car_id/consumptions/:id(.:format) consumptions#update
DELETE /cars/:car_id/consumptions/:id(.:format) consumptions#destroy


As requested
enter image description here



EDIT



here is what I have in the HTML if it can help:



<form novalidate="novalidate" class="simple_form /cars/1/consumptions" action="/cars/1/consumptions/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="0nwq/pQSXCU2ptBjbewTCBffPLpZZUPAj6/HPQTGtYd8cHz9zv8R/C/JYnXPDpKw5o3/vGlVtav2Sa2nSvgOQdQ==">









share|improve this question
















I have this routing error when I add a consumption (after submit it fails) and I am stuck, What am I doing wrong?



A user can have several cars and for each of his car he wants to look after his gas consumptions.



I have three active record models



create_table "cars", force: :cascade do |t|
t.string "car_name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumption_searches", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "consumptions", force: :cascade do |t|
t.float "total_price"
t.float "kilometers"
t.string "shop"
t.float "liter_price"
t.float "total_liters"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "car_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end



  • car belongs_to :users and has_many :consumptions

  • user has_many :cars, and has_many :consumptions through: :cars

  • consumption belong_to :car and belongs_to :user


My create method in consumptions_controller.rb



def create 
@car = Car.find(params[:car_id])
@consumption = Consumptions.new(consumption_params)
@consumption.car = @car
if @consumption.save!
redirect_to car_consumptions_path, notice: 'consumption was successfully created.'
else
render :new
end
end


cars_controller.rb



def show
@car = Car.find(params[:id])
@search = ConsumptionSearch.new(params[:search])
@consumptions = @search.date_range
@consumptions = @consumptions.order('created_at ASC').where(car_id: @car.id)
end


views/consumptions/_form.html.erb



<%= simple_form_for car_consumptions_path do |f| %>
...
<% end %>


routes.rb



Rails.application.routes.draw do
devise_for :users
root to: "cars#index"

resources :cars do
resources :consumptions
end
end


rails routes | grep consumption



  car_consumptions GET    /cars/:car_id/consumptions(.:format)                                                     consumptions#index
POST /cars/:car_id/consumptions(.:format) consumptions#create
new_car_consumption GET /cars/:car_id/consumptions/new(.:format) consumptions#new
edit_car_consumption GET /cars/:car_id/consumptions/:id/edit(.:format) consumptions#edit
car_consumption GET /cars/:car_id/consumptions/:id(.:format) consumptions#show
PATCH /cars/:car_id/consumptions/:id(.:format) consumptions#update
PUT /cars/:car_id/consumptions/:id(.:format) consumptions#update
DELETE /cars/:car_id/consumptions/:id(.:format) consumptions#destroy


As requested
enter image description here



EDIT



here is what I have in the HTML if it can help:



<form novalidate="novalidate" class="simple_form /cars/1/consumptions" action="/cars/1/consumptions/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="0nwq/pQSXCU2ptBjbewTCBffPLpZZUPAj6/HPQTGtYd8cHz9zv8R/C/JYnXPDpKw5o3/vGlVtav2Sa2nSvgOQdQ==">






ruby-on-rails ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:42







johan

















asked Nov 20 '18 at 16:46









johanjohan

255110




255110













  • What's the exact line that shows the error (show the full stacktrace)? because the error points to a "new" action but you are not using new_car_consumption anywhere. The error says that, somewhere, you are trying to point to new_car_consumption using method POST which is not right, it should be GET.

    – arieljuod
    Nov 20 '18 at 17:11













  • updated, it's after submiting the new consumption

    – johan
    Nov 20 '18 at 17:16











  • car_consumptions_path is missing a car_id on both places you use it, also simple_form_for expect the first parameter to be an object and you are passing a helper, it should be something like: simple_form_for [@consumption, @car] for nested resources. You are still not showing the full error stacktrace (at least file name and line number)

    – arieljuod
    Nov 20 '18 at 17:27











  • The picture of the error page is not showing the stacktrace of the error. Click on Full Trace

    – arieljuod
    Nov 20 '18 at 18:51



















  • What's the exact line that shows the error (show the full stacktrace)? because the error points to a "new" action but you are not using new_car_consumption anywhere. The error says that, somewhere, you are trying to point to new_car_consumption using method POST which is not right, it should be GET.

    – arieljuod
    Nov 20 '18 at 17:11













  • updated, it's after submiting the new consumption

    – johan
    Nov 20 '18 at 17:16











  • car_consumptions_path is missing a car_id on both places you use it, also simple_form_for expect the first parameter to be an object and you are passing a helper, it should be something like: simple_form_for [@consumption, @car] for nested resources. You are still not showing the full error stacktrace (at least file name and line number)

    – arieljuod
    Nov 20 '18 at 17:27











  • The picture of the error page is not showing the stacktrace of the error. Click on Full Trace

    – arieljuod
    Nov 20 '18 at 18:51

















What's the exact line that shows the error (show the full stacktrace)? because the error points to a "new" action but you are not using new_car_consumption anywhere. The error says that, somewhere, you are trying to point to new_car_consumption using method POST which is not right, it should be GET.

– arieljuod
Nov 20 '18 at 17:11







What's the exact line that shows the error (show the full stacktrace)? because the error points to a "new" action but you are not using new_car_consumption anywhere. The error says that, somewhere, you are trying to point to new_car_consumption using method POST which is not right, it should be GET.

– arieljuod
Nov 20 '18 at 17:11















updated, it's after submiting the new consumption

– johan
Nov 20 '18 at 17:16





updated, it's after submiting the new consumption

– johan
Nov 20 '18 at 17:16













car_consumptions_path is missing a car_id on both places you use it, also simple_form_for expect the first parameter to be an object and you are passing a helper, it should be something like: simple_form_for [@consumption, @car] for nested resources. You are still not showing the full error stacktrace (at least file name and line number)

– arieljuod
Nov 20 '18 at 17:27





car_consumptions_path is missing a car_id on both places you use it, also simple_form_for expect the first parameter to be an object and you are passing a helper, it should be something like: simple_form_for [@consumption, @car] for nested resources. You are still not showing the full error stacktrace (at least file name and line number)

– arieljuod
Nov 20 '18 at 17:27













The picture of the error page is not showing the stacktrace of the error. Click on Full Trace

– arieljuod
Nov 20 '18 at 18:51





The picture of the error page is not showing the stacktrace of the error. Click on Full Trace

– arieljuod
Nov 20 '18 at 18:51












4 Answers
4






active

oldest

votes


















1














Lets start with the controller



# GET /cars/:car_id/consumptions/new
def new
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new
end

# POST /cars/:car_id/consumptions
def create
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new(consumption_params)
# `.save!` will raise an exception and blow up if the record is invalid.
# Not good.
if @consumption.save
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
else
render :new
end
end


Note the redirect:



# consumptions#index
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'


Since this is a nested route you need to provide the car_id segment.



You could also redirect to:



# consumptions#show
redirect_to [@car, @consumption], notice: 'consumption was successfully created.'
# or to cars#show
redirect_to @car, notice: 'consumption was successfully created.'


When using simple_form_for you pass it model instances that it binds the form to. When creating forms for nested routes you should pass an array:



<%= simple_form_for([@car, @consumption]) do |f| %>

<% end %>


This uses the polymorphic route helpers to find the correct path. You can use this same signature for link_to, redirect_to, url_for and path_for.



When declaring nested routes you should consider using the shallow option. It will only nest the collection routes (new, create, index) and not the member routes.






share|improve this answer


























  • Many thanks I could figure before finding your reply that I was missing @car = Car.find(params[:car_id])in the new action. Your help was really precious for the redirection ! + 10

    – johan
    Nov 20 '18 at 22:20













  • @max I'm not sure you can call new on the association, there's a build method for that.

    – arieljuod
    Nov 21 '18 at 1:40











  • @arieljuod .build is just an alias for .new

    – max
    Nov 21 '18 at 16:03



















1














Try this:



 <%= form_for [@cars, @consumptions] do |form| %>
...
<% end %>


or



<%= form_with(model: [ @cars, @consumptions ]) do |form| %>


*** Update:



resources :cars, shallow: true do
resources :consumptions
end


Into form:



<% = simple_form_for [@car, @consumption] do |f| %>





share|improve this answer


























  • it doesn't work but thanks for trying

    – johan
    Nov 20 '18 at 17:36



















1














You are not showing your new action, but I assume you are setting a @car and a @consumption = @car.consumptions.build variables.



Try with this:



simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|


It should work with simple_form_for [@car, @consumption] do |f| but you said "it doesn't work" which is too ambiguous (how does that not work? same error? new error? you should be more clear when responding to answers)






share|improve this answer
























  • I was missing @car in my new action...

    – johan
    Nov 20 '18 at 22:39



















0














 car_consumptions_path != car_consumption_path 


I don't see any "car_consumptions" in your routes.






share|improve this answer
























  • I changed to car_consumption_path No route matches {:action=>"show", :car_id=>"1", :controller=>"consumptions"}, missing required keys: [:id]

    – johan
    Nov 20 '18 at 17:22











  • but there are: car_consumptions GET /cars/:car_id/consumptions(.:format) consumptions#index POST /cars/:car_id/consumptions(.:format)

    – johan
    Nov 20 '18 at 17:27











  • I guess you need: car_consumption_path(car, consumption) because you need both ":id" parts.

    – aarkerio
    Nov 20 '18 at 17:55











  • it does not do the job :(

    – johan
    Nov 20 '18 at 18:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397702%2factioncontrollerroutingerror-no-route-matches-post-cars-1-consumptions-ne%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Lets start with the controller



# GET /cars/:car_id/consumptions/new
def new
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new
end

# POST /cars/:car_id/consumptions
def create
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new(consumption_params)
# `.save!` will raise an exception and blow up if the record is invalid.
# Not good.
if @consumption.save
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
else
render :new
end
end


Note the redirect:



# consumptions#index
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'


Since this is a nested route you need to provide the car_id segment.



You could also redirect to:



# consumptions#show
redirect_to [@car, @consumption], notice: 'consumption was successfully created.'
# or to cars#show
redirect_to @car, notice: 'consumption was successfully created.'


When using simple_form_for you pass it model instances that it binds the form to. When creating forms for nested routes you should pass an array:



<%= simple_form_for([@car, @consumption]) do |f| %>

<% end %>


This uses the polymorphic route helpers to find the correct path. You can use this same signature for link_to, redirect_to, url_for and path_for.



When declaring nested routes you should consider using the shallow option. It will only nest the collection routes (new, create, index) and not the member routes.






share|improve this answer


























  • Many thanks I could figure before finding your reply that I was missing @car = Car.find(params[:car_id])in the new action. Your help was really precious for the redirection ! + 10

    – johan
    Nov 20 '18 at 22:20













  • @max I'm not sure you can call new on the association, there's a build method for that.

    – arieljuod
    Nov 21 '18 at 1:40











  • @arieljuod .build is just an alias for .new

    – max
    Nov 21 '18 at 16:03
















1














Lets start with the controller



# GET /cars/:car_id/consumptions/new
def new
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new
end

# POST /cars/:car_id/consumptions
def create
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new(consumption_params)
# `.save!` will raise an exception and blow up if the record is invalid.
# Not good.
if @consumption.save
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
else
render :new
end
end


Note the redirect:



# consumptions#index
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'


Since this is a nested route you need to provide the car_id segment.



You could also redirect to:



# consumptions#show
redirect_to [@car, @consumption], notice: 'consumption was successfully created.'
# or to cars#show
redirect_to @car, notice: 'consumption was successfully created.'


When using simple_form_for you pass it model instances that it binds the form to. When creating forms for nested routes you should pass an array:



<%= simple_form_for([@car, @consumption]) do |f| %>

<% end %>


This uses the polymorphic route helpers to find the correct path. You can use this same signature for link_to, redirect_to, url_for and path_for.



When declaring nested routes you should consider using the shallow option. It will only nest the collection routes (new, create, index) and not the member routes.






share|improve this answer


























  • Many thanks I could figure before finding your reply that I was missing @car = Car.find(params[:car_id])in the new action. Your help was really precious for the redirection ! + 10

    – johan
    Nov 20 '18 at 22:20













  • @max I'm not sure you can call new on the association, there's a build method for that.

    – arieljuod
    Nov 21 '18 at 1:40











  • @arieljuod .build is just an alias for .new

    – max
    Nov 21 '18 at 16:03














1












1








1







Lets start with the controller



# GET /cars/:car_id/consumptions/new
def new
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new
end

# POST /cars/:car_id/consumptions
def create
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new(consumption_params)
# `.save!` will raise an exception and blow up if the record is invalid.
# Not good.
if @consumption.save
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
else
render :new
end
end


Note the redirect:



# consumptions#index
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'


Since this is a nested route you need to provide the car_id segment.



You could also redirect to:



# consumptions#show
redirect_to [@car, @consumption], notice: 'consumption was successfully created.'
# or to cars#show
redirect_to @car, notice: 'consumption was successfully created.'


When using simple_form_for you pass it model instances that it binds the form to. When creating forms for nested routes you should pass an array:



<%= simple_form_for([@car, @consumption]) do |f| %>

<% end %>


This uses the polymorphic route helpers to find the correct path. You can use this same signature for link_to, redirect_to, url_for and path_for.



When declaring nested routes you should consider using the shallow option. It will only nest the collection routes (new, create, index) and not the member routes.






share|improve this answer















Lets start with the controller



# GET /cars/:car_id/consumptions/new
def new
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new
end

# POST /cars/:car_id/consumptions
def create
@car = Car.find(params[:car_id])
@consumption = @car.consumptions.new(consumption_params)
# `.save!` will raise an exception and blow up if the record is invalid.
# Not good.
if @consumption.save
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
else
render :new
end
end


Note the redirect:



# consumptions#index
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'


Since this is a nested route you need to provide the car_id segment.



You could also redirect to:



# consumptions#show
redirect_to [@car, @consumption], notice: 'consumption was successfully created.'
# or to cars#show
redirect_to @car, notice: 'consumption was successfully created.'


When using simple_form_for you pass it model instances that it binds the form to. When creating forms for nested routes you should pass an array:



<%= simple_form_for([@car, @consumption]) do |f| %>

<% end %>


This uses the polymorphic route helpers to find the correct path. You can use this same signature for link_to, redirect_to, url_for and path_for.



When declaring nested routes you should consider using the shallow option. It will only nest the collection routes (new, create, index) and not the member routes.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 21:29

























answered Nov 20 '18 at 21:21









maxmax

46.2k1060104




46.2k1060104













  • Many thanks I could figure before finding your reply that I was missing @car = Car.find(params[:car_id])in the new action. Your help was really precious for the redirection ! + 10

    – johan
    Nov 20 '18 at 22:20













  • @max I'm not sure you can call new on the association, there's a build method for that.

    – arieljuod
    Nov 21 '18 at 1:40











  • @arieljuod .build is just an alias for .new

    – max
    Nov 21 '18 at 16:03



















  • Many thanks I could figure before finding your reply that I was missing @car = Car.find(params[:car_id])in the new action. Your help was really precious for the redirection ! + 10

    – johan
    Nov 20 '18 at 22:20













  • @max I'm not sure you can call new on the association, there's a build method for that.

    – arieljuod
    Nov 21 '18 at 1:40











  • @arieljuod .build is just an alias for .new

    – max
    Nov 21 '18 at 16:03

















Many thanks I could figure before finding your reply that I was missing @car = Car.find(params[:car_id])in the new action. Your help was really precious for the redirection ! + 10

– johan
Nov 20 '18 at 22:20







Many thanks I could figure before finding your reply that I was missing @car = Car.find(params[:car_id])in the new action. Your help was really precious for the redirection ! + 10

– johan
Nov 20 '18 at 22:20















@max I'm not sure you can call new on the association, there's a build method for that.

– arieljuod
Nov 21 '18 at 1:40





@max I'm not sure you can call new on the association, there's a build method for that.

– arieljuod
Nov 21 '18 at 1:40













@arieljuod .build is just an alias for .new

– max
Nov 21 '18 at 16:03





@arieljuod .build is just an alias for .new

– max
Nov 21 '18 at 16:03













1














Try this:



 <%= form_for [@cars, @consumptions] do |form| %>
...
<% end %>


or



<%= form_with(model: [ @cars, @consumptions ]) do |form| %>


*** Update:



resources :cars, shallow: true do
resources :consumptions
end


Into form:



<% = simple_form_for [@car, @consumption] do |f| %>





share|improve this answer


























  • it doesn't work but thanks for trying

    – johan
    Nov 20 '18 at 17:36
















1














Try this:



 <%= form_for [@cars, @consumptions] do |form| %>
...
<% end %>


or



<%= form_with(model: [ @cars, @consumptions ]) do |form| %>


*** Update:



resources :cars, shallow: true do
resources :consumptions
end


Into form:



<% = simple_form_for [@car, @consumption] do |f| %>





share|improve this answer


























  • it doesn't work but thanks for trying

    – johan
    Nov 20 '18 at 17:36














1












1








1







Try this:



 <%= form_for [@cars, @consumptions] do |form| %>
...
<% end %>


or



<%= form_with(model: [ @cars, @consumptions ]) do |form| %>


*** Update:



resources :cars, shallow: true do
resources :consumptions
end


Into form:



<% = simple_form_for [@car, @consumption] do |f| %>





share|improve this answer















Try this:



 <%= form_for [@cars, @consumptions] do |form| %>
...
<% end %>


or



<%= form_with(model: [ @cars, @consumptions ]) do |form| %>


*** Update:



resources :cars, shallow: true do
resources :consumptions
end


Into form:



<% = simple_form_for [@car, @consumption] do |f| %>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 17:51

























answered Nov 20 '18 at 17:28









Raf4Raf4

113




113













  • it doesn't work but thanks for trying

    – johan
    Nov 20 '18 at 17:36



















  • it doesn't work but thanks for trying

    – johan
    Nov 20 '18 at 17:36

















it doesn't work but thanks for trying

– johan
Nov 20 '18 at 17:36





it doesn't work but thanks for trying

– johan
Nov 20 '18 at 17:36











1














You are not showing your new action, but I assume you are setting a @car and a @consumption = @car.consumptions.build variables.



Try with this:



simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|


It should work with simple_form_for [@car, @consumption] do |f| but you said "it doesn't work" which is too ambiguous (how does that not work? same error? new error? you should be more clear when responding to answers)






share|improve this answer
























  • I was missing @car in my new action...

    – johan
    Nov 20 '18 at 22:39
















1














You are not showing your new action, but I assume you are setting a @car and a @consumption = @car.consumptions.build variables.



Try with this:



simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|


It should work with simple_form_for [@car, @consumption] do |f| but you said "it doesn't work" which is too ambiguous (how does that not work? same error? new error? you should be more clear when responding to answers)






share|improve this answer
























  • I was missing @car in my new action...

    – johan
    Nov 20 '18 at 22:39














1












1








1







You are not showing your new action, but I assume you are setting a @car and a @consumption = @car.consumptions.build variables.



Try with this:



simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|


It should work with simple_form_for [@car, @consumption] do |f| but you said "it doesn't work" which is too ambiguous (how does that not work? same error? new error? you should be more clear when responding to answers)






share|improve this answer













You are not showing your new action, but I assume you are setting a @car and a @consumption = @car.consumptions.build variables.



Try with this:



simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|


It should work with simple_form_for [@car, @consumption] do |f| but you said "it doesn't work" which is too ambiguous (how does that not work? same error? new error? you should be more clear when responding to answers)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 19:02









arieljuodarieljuod

7,37311221




7,37311221













  • I was missing @car in my new action...

    – johan
    Nov 20 '18 at 22:39



















  • I was missing @car in my new action...

    – johan
    Nov 20 '18 at 22:39

















I was missing @car in my new action...

– johan
Nov 20 '18 at 22:39





I was missing @car in my new action...

– johan
Nov 20 '18 at 22:39











0














 car_consumptions_path != car_consumption_path 


I don't see any "car_consumptions" in your routes.






share|improve this answer
























  • I changed to car_consumption_path No route matches {:action=>"show", :car_id=>"1", :controller=>"consumptions"}, missing required keys: [:id]

    – johan
    Nov 20 '18 at 17:22











  • but there are: car_consumptions GET /cars/:car_id/consumptions(.:format) consumptions#index POST /cars/:car_id/consumptions(.:format)

    – johan
    Nov 20 '18 at 17:27











  • I guess you need: car_consumption_path(car, consumption) because you need both ":id" parts.

    – aarkerio
    Nov 20 '18 at 17:55











  • it does not do the job :(

    – johan
    Nov 20 '18 at 18:42
















0














 car_consumptions_path != car_consumption_path 


I don't see any "car_consumptions" in your routes.






share|improve this answer
























  • I changed to car_consumption_path No route matches {:action=>"show", :car_id=>"1", :controller=>"consumptions"}, missing required keys: [:id]

    – johan
    Nov 20 '18 at 17:22











  • but there are: car_consumptions GET /cars/:car_id/consumptions(.:format) consumptions#index POST /cars/:car_id/consumptions(.:format)

    – johan
    Nov 20 '18 at 17:27











  • I guess you need: car_consumption_path(car, consumption) because you need both ":id" parts.

    – aarkerio
    Nov 20 '18 at 17:55











  • it does not do the job :(

    – johan
    Nov 20 '18 at 18:42














0












0








0







 car_consumptions_path != car_consumption_path 


I don't see any "car_consumptions" in your routes.






share|improve this answer













 car_consumptions_path != car_consumption_path 


I don't see any "car_consumptions" in your routes.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 17:20









aarkerioaarkerio

7891621




7891621













  • I changed to car_consumption_path No route matches {:action=>"show", :car_id=>"1", :controller=>"consumptions"}, missing required keys: [:id]

    – johan
    Nov 20 '18 at 17:22











  • but there are: car_consumptions GET /cars/:car_id/consumptions(.:format) consumptions#index POST /cars/:car_id/consumptions(.:format)

    – johan
    Nov 20 '18 at 17:27











  • I guess you need: car_consumption_path(car, consumption) because you need both ":id" parts.

    – aarkerio
    Nov 20 '18 at 17:55











  • it does not do the job :(

    – johan
    Nov 20 '18 at 18:42



















  • I changed to car_consumption_path No route matches {:action=>"show", :car_id=>"1", :controller=>"consumptions"}, missing required keys: [:id]

    – johan
    Nov 20 '18 at 17:22











  • but there are: car_consumptions GET /cars/:car_id/consumptions(.:format) consumptions#index POST /cars/:car_id/consumptions(.:format)

    – johan
    Nov 20 '18 at 17:27











  • I guess you need: car_consumption_path(car, consumption) because you need both ":id" parts.

    – aarkerio
    Nov 20 '18 at 17:55











  • it does not do the job :(

    – johan
    Nov 20 '18 at 18:42

















I changed to car_consumption_path No route matches {:action=>"show", :car_id=>"1", :controller=>"consumptions"}, missing required keys: [:id]

– johan
Nov 20 '18 at 17:22





I changed to car_consumption_path No route matches {:action=>"show", :car_id=>"1", :controller=>"consumptions"}, missing required keys: [:id]

– johan
Nov 20 '18 at 17:22













but there are: car_consumptions GET /cars/:car_id/consumptions(.:format) consumptions#index POST /cars/:car_id/consumptions(.:format)

– johan
Nov 20 '18 at 17:27





but there are: car_consumptions GET /cars/:car_id/consumptions(.:format) consumptions#index POST /cars/:car_id/consumptions(.:format)

– johan
Nov 20 '18 at 17:27













I guess you need: car_consumption_path(car, consumption) because you need both ":id" parts.

– aarkerio
Nov 20 '18 at 17:55





I guess you need: car_consumption_path(car, consumption) because you need both ":id" parts.

– aarkerio
Nov 20 '18 at 17:55













it does not do the job :(

– johan
Nov 20 '18 at 18:42





it does not do the job :(

– johan
Nov 20 '18 at 18:42


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397702%2factioncontrollerroutingerror-no-route-matches-post-cars-1-consumptions-ne%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)