Last active
May 20, 2017 14:23
-
-
Save caseydriscoll/7eaa239f9ca967cb1bc6724c8ca751cc to your computer and use it in GitHub Desktop.
Rails console output from 'Issue Generating Objects Indirectly from Patch Controller Method'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Print < ApplicationRecord | |
has_many :project_print | |
has_many :projects, :through => :project_print | |
attr_accessor :title, :left_photo_id, :right_photo_id | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project < ApplicationRecord | |
has_many :project_print | |
has_many :prints, :through => :project_print | |
accepts_nested_attributes_for :prints | |
validates_presence_of :title | |
def generate | |
puts '>> generating the prints' | |
photos = Photo.all | |
photos.each_with_index do |photo, index| | |
puts ">> #{index}" | |
if index.odd? | |
even = photos[index - 1] | |
pprint = Print.new | |
pprint.title = "the title" | |
pprint.left_photo_id = even.id | |
pprint.right_photo_id = photo.id | |
puts ">> print.inspect #{pprint.inspect}" | |
pprint.save! | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProjectsController < ApplicationController | |
before_action :set_project, only: [:generate, :show, :edit, :update, :destroy] | |
# GET /projects | |
# GET /projects.json | |
def index | |
@projects = Project.all | |
end | |
# GET /projects/1 | |
# GET /projects/1.json | |
def show | |
end | |
# GET /projects/new | |
def new | |
@project = Project.new | |
end | |
# GET /projects/1/edit | |
def edit | |
end | |
# POST /projects | |
# POST /projects.json | |
def create | |
@project = Project.new(project_params) | |
respond_to do |format| | |
if @project.save | |
format.html { redirect_to @project, notice: 'Project was successfully created.' } | |
format.json { render :show, status: :created, location: @project } | |
else | |
format.html { render :new } | |
format.json { render json: @project.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /projects/1/generate | |
def generate | |
respond_to do |format| | |
if @project.generate | |
format.html { redirect_to @project, notice: 'Project successfully generated prints.' } | |
format.json { render :show, status: :ok, location: @project } | |
else | |
format.html { render :edit } | |
format.json { render json: @project.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /projects/1 | |
# PATCH/PUT /projects/1.json | |
def update | |
respond_to do |format| | |
if @project.update(project_params) | |
format.html { redirect_to @project, notice: 'Project was successfully updated.' } | |
format.json { render :show, status: :ok, location: @project } | |
else | |
format.html { render :edit } | |
format.json { render json: @project.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /projects/1 | |
# DELETE /projects/1.json | |
def destroy | |
@project.destroy | |
respond_to do |format| | |
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_project | |
@project = Project.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def project_params | |
params.require(:project).permit(:title, :project_print) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Started PATCH "/projects/2/generate" for ::1 at 2017-05-20 10:17:40 -0400 | |
Processing by ProjectsController#generate as HTML | |
Parameters: {"utf8"=>"✓", "authenticity_token"=>"uu0E0/6ysaJxYzGEQqMscDbmNbFWVSFgV7mKaFdRX4N48swlAUOW0cxuMjCg9SAa6+XiyflHXdZVsPxxym3vKw==", "id"=>"2"} | |
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] | |
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] | |
>> in generate | |
>> generating the prints | |
Photo Load (0.1ms) SELECT "photos".* FROM "photos" | |
>> 0 | |
>> 1 | |
>> print.inspect #<Print id: nil, title: nil, projectprint_id: nil, printphoto_id: nil, created_at: nil, updated_at: nil, left_photo_id: nil, right_photo_id: nil> | |
(0.2ms) begin transaction | |
SQL (2.1ms) INSERT INTO "prints" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-05-20 14:17:40 UTC], ["updated_at", 2017-05-20 14:17:40 UTC]] | |
(0.8ms) commit transaction | |
>> 2 | |
>> 3 | |
>> print.inspect #<Print id: nil, title: nil, projectprint_id: nil, printphoto_id: nil, created_at: nil, updated_at: nil, left_photo_id: nil, right_photo_id: nil> | |
(0.0ms) begin transaction | |
SQL (0.2ms) INSERT INTO "prints" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-05-20 14:17:40 UTC], ["updated_at", 2017-05-20 14:17:40 UTC]] | |
(0.6ms) commit transaction | |
>> 4 | |
Redirected to http://localhost:3000/projects/2 | |
Completed 302 Found in 41ms (ActiveRecord: 5.8ms) | |
Started GET "/projects/2" for ::1 at 2017-05-20 10:17:40 -0400 | |
Processing by ProjectsController#show as HTML | |
Parameters: {"id"=>"2"} | |
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] | |
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] | |
Rendering projects/show.html.erb within layouts/application | |
(0.2ms) SELECT COUNT(*) FROM "prints" INNER JOIN "project_prints" ON "prints"."id" = "project_prints"."print_id" WHERE "project_prints"."project_id" = ? [["project_id", 2]] | |
CACHE (0.0ms) SELECT COUNT(*) FROM "prints" INNER JOIN "project_prints" ON "prints"."id" = "project_prints"."print_id" WHERE "project_prints"."project_id" = ? [["project_id", 2]] | |
Rendered projects/show.html.erb within layouts/application (5.0ms) | |
Completed 200 OK in 51ms (Views: 46.7ms | ActiveRecord: 0.6ms) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
resources :projects do | |
resources :prints | |
end | |
resources :prints | |
resources :photos | |
get 'home/index' | |
patch 'projects/:id/generate', to: 'projects#generate' | |
put 'projects/:id/generate', to: 'projects#generate' | |
devise_for :users | |
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | |
root to: "home#index" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment