Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tbuehlmann/820672db80e8d28f73c1b0f45fe5ebaa to your computer and use it in GitHub Desktop.
Save tbuehlmann/820672db80e8d28f73c1b0f45fe5ebaa to your computer and use it in GitHub Desktop.
class CreateReleaseSteps < ActiveRecord::Migration[5.0]
def change
create_table :release_steps do |t|
t.belongs_to :release, index: true
t.string :name
t.string :loadbalancer
t.string :comment
t.integer :release_id
end
end
end
# app/model/release.rb
class Release < ApplicationRecord
has_many :release_steps
end
# app/model/release_step.rb
class ReleaseStep < ApplicationRecord
belongs_to :release
end
class ReleaseStepsController < ApplicationController
before_action :set_release
def create
@release_step = @release.release_steps.create(release_step_params)
if @release_step.save
render json: @release_step, status: :created, location: @release_step
else
render json: @release_step.errors, status: :unprocessable_entity
end
end
private
def set_release
@release = Release.find(params[:release_id])
end
end
# config/routes
Rails.application.routes.draw do
resources :releases do
resources :release_steps
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
# HTTP call to create a release step
curl localhost:3000/release_steps -X POST -H "Content-Type: application/json" -d '{"name": "test-name", "loadbalancer":"test-lb","comment":"test-comment"}'
# So...shouldn't I be able to do this in the ReleaseStep controller?
def create
@release_step = @release.release_steps.create(release_step_params)
if @release_step.save
render json: @release_step, status: :created, location: @release_step
else
render json: @release_step.errors, status: :unprocessable_entity
end
end
# Because I get this
NoMethodError: undefined method `release_steps' for nil:NilClass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment