Skip to content

Instantly share code, notes, and snippets.

@brandoncordell
Last active August 29, 2015 14:04
Show Gist options
  • Save brandoncordell/fca1e9aae4eeeb91e244 to your computer and use it in GitHub Desktop.
Save brandoncordell/fca1e9aae4eeeb91e244 to your computer and use it in GitHub Desktop.
missing required keys: [:user_id]
# Routes
Prefix Verb URI Pattern Controller#Action
sidekiq_web /sidekiq Sidekiq::Web
root GET / high_voltage/pages#show {:id=>"home"}
user_bank_accounts GET /users/:user_id/bank_accounts(.:format) bank_accounts#index
POST /users/:user_id/bank_accounts(.:format) bank_accounts#create
new_user_bank_account GET /users/:user_id/bank_accounts/new(.:format) bank_accounts#new
edit_user_bank_account GET /users/:user_id/bank_accounts/:id/edit(.:format) bank_accounts#edit
user_bank_account GET /users/:user_id/bank_accounts/:id(.:format) bank_accounts#show
PATCH /users/:user_id/bank_accounts/:id(.:format) bank_accounts#update
PUT /users/:user_id/bank_accounts/:id(.:format) bank_accounts#update
DELETE /users/:user_id/bank_accounts/:id(.:format) bank_accounts#destroy
user_products GET /users/:user_id/products(.:format) products#index
POST /users/:user_id/products(.:format) products#create
new_user_product GET /users/:user_id/products/new(.:format) products#new
edit_user_product GET /users/:user_id/products/:id/edit(.:format) products#edit
user_product GET /users/:user_id/products/:id(.:format) products#show
PATCH /users/:user_id/products/:id(.:format) products#update
PUT /users/:user_id/products/:id(.:format) products#update
DELETE /users/:user_id/products/:id(.:format) products#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
login GET /login(.:format) sessions#new
post_login POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
page GET /*id high_voltage/pages#show
# app/controllers/products_controller.rb
class ProductsController < ApplicationController
...
def create
@user = Product.new(product_params)
if @user.save
redirect_to user_products_path(current_user), notice: 'Product has been uploaded'
else
render :new
end
end
...
private
def product_params
params.require(:product).permit(:status, :name, :description, :price_cents, :price_currency)
end
end
# spec/controllers/products_controller_spec.rb
require 'rails_helper'
describe ProductsController do
let(:user) { create :user }
describe 'GET new' do
before { get :new, user_id: user.id }
it 'assigns @user' do
expect(assigns(:user)).to be_a Product
end
end
describe 'POST create' do
context 'with valid params' do
let(:valid_product) { attributes_for :product }
it 'redirects to user_products_path' do
post :create, user_id: user.id, product: valid_product # this is the line it breaks on
expect(response).to redirect_to(user_products_path(user))
end
end
context 'with invalid params' do
let(:invalid_product) { attributes_for(:product, name: '') }
it 'renders :new' do
post :create, user_id: user.id, product: invalid_product # this works just fine
expect(response).to render_template(:new)
end
end
end
end
# RSpec output
# ActionController::UrlGenerationError:
# No route matches {:action=>"index", :controller=>"products", :user_id=>nil} missing required keys: [:user_id]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment