2016-09-18 3 views
1

Ich folge einem LYNDA.COM Tutorial mit Kevin Skoglund. Ich folgte zusammen in den „Many-to-many“ Assoziationen: Reiche“Video, wenn ich ein Problem hatte, die letzte Zeile Ich lief (section.section_edits) ergab folgendes:.section_id: null WENN ES SOLLTE section_id: 1

SectionEdit id: 6, admin_user_id: 1, section_id: nil, ... 

jedoch Kevins Ergebnis auf dem Tutorial war:

SectionEdit id: 1, admin_user_id: 1, section_id: 1, ... 

Offensichtlich habe ich ging mehrmals wieder zu versuchen, meine Fehler zu finden, weshalb mein SectionEdit id: unterscheidet sich von seinen Aber meine Frage ist:. Deshalb ist mein section_id: nil

EDIT: 09/25/16

Ich habe section.errors in meine Eingabeaufforderungen eingegeben, verstehe aber die Konsolenantwort nicht vollständig. @AlterLagos

irb(main):004:0> section = Section.create(:name => "Section One", :position => 1) (0.5ms) BEGIN (1.0ms) ROLLBACK => #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil> irb(main):005:0> section.errors => #<ActiveModel::Errors:0x6340fe0 @base=#<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil>, @messages={:page=>["must exist"]}, @details={:page=>[{:error=>:blank}]}>

Unten ist mein consol und zugehörigen Klassen. VIELEN DANK!

MY COMMAND PROMPT

C:\Users\David\My Documents\sites\simple_cms>rails console 
Loading development environment (Rails 5.0.0.1) 

irb(main):001:0> me = AdminUser.find(1) 
    AdminUser Load (4.0ms) SELECT `admin_users`.* FROM `admin_users` WHERE`admin_users`.`id` = 1 LIMIT 1 
=> #<AdminUser id: 1, first_name: "David", last_name: "Boyette", email: "", hashed_password: nil, created_at: "2016-09-08 02:52:57", updated_at: "2016-09-08 02:52:57", username: "dboyette", salt: nil> 

irb(main):002:0> me.section_edits 
    SectionEdit Load (3.5ms) SELECT `section_edits`.* FROM `section_edits` WHERE `section_edits`.`admin_user_id` = 1 
=> #<ActiveRecord::Associations::CollectionProxy [#<SectionEdit id: 1, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-09 01:19:08", updated_at: "2016-09-09 01:19:08">, #<SectionEdit id: 2, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-10 00:05:56", updated_at: "2016-09-10 00:05:56">, #<SectionEdit id: 3, admin_user_id: 1, section_id: nil, summary: "Ch-ch-ch-changes", created_at: "2016-09-10 00:09:39", updated_at: "2016-09-10 00:09:39">, #<SectionEdit id: 4, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-17 03:32:13", updated_at: "2016-09-17 03:32:13">, #<SectionEdit id: 5, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-18 00:48:40", updated_at: "2016-09-18 00:48:40">]> 

irb(main):003:0> section = Section.create(:name => "Section One", :position => 1) 
    (1.0ms) BEGIN 
    (1.0ms) ROLLBACK 
=> #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil> 

irb(main):004:0> section.section_edits => #<ActiveRecord::Associations::CollectionProxy []> 

irb(main):005:0> edit = SectionEdit.new 
=> #<SectionEdit id: nil, admin_user_id: nil, section_id: nil, summary: nil, created_at: nil, updated_at: nil> 

irb(main):006:0> edit.summary = "Test edit" 
=> "Test edit" 

irb(main):007:0> section.section_edits << edit 
=> #<ActiveRecord::Associations::CollectionProxy [#<SectionEdit id: nil, admin_user_id: nil, section_id: nil, summary: "Test edit", created_at: nil, updated_at: nil>]> 

irb(main):008:0> edit.editor = me 
=> #<AdminUser id: 1, first_name: "David", last_name: "Boyette", email: "", hashed_password: nil, created_at: "2016-09-08 02:52:57", updated_at: "2016-09-08 02:52:57", username: "dboyette", salt: nil> 

irb(main):009:0> edit.save 
    (1.5ms) BEGIN 
    SQL (3.0ms) INSERT INTO `section_edits` (`admin_user_id`, `summary`, `created_at`, `updated_at`) VALUES (1, 'Test edit', '2016-09-18 01:44:41', '2016-09-18 01:44:41') 
    (69.3ms) COMMIT 
=> true 

irb(main):010:0> section.section_edits 
=> #<ActiveRecord::Associations::CollectionProxy [#<SectionEdit id: 6, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-18 01:44:41", updated_at: "2016-09-18 01:44:41">]> 

irb(main):011:0> 

section.rb

class Section < ApplicationRecord 

belongs_to :page 
has_many :section_edits 
has_many :editors, :through => :section_edits, :class_name => "AdminUser" 

Ende

Sections Migration

class CreateSections < ActiveRecord::Migration[5.0] 

def ändern create_table: Abschnitte tun | t |

t.integer "page_id" 
    # same as t.reference :page 
    t.string "name" 
    t.integer "position" 
    t.boolean "visible", :default => false 
    t.string "content_type" 
    t.text "content" 
    t.timestamps 

end 

add_index("sections", "page_id") 

Ende Ende

section_edit.rb

class SectionEdit < ApplicationRecord 

belongs_to :editor, :class_name => "AdminUser", :foreign_key => 'admin_user_id' 
belongs_to :section 

Ende

section_edits Migration

class CreateSectionEdits < ActiveRecord::Migration[5.0] 

def Änderung create_table: section_edits do | t |

t.references :admin_user 
t.references :section 
t.string "summary" 
t.timestamps 

end 
add_index :section_edits, ['admin_user_id', 'section_id'] 

Ende Ende

Modell: Seite.rb

`Klasse Seite < ApplicationRecord

belongs_to :subject 
has_many :sections 
has_and_belongs_to_many :editors, :class_name => "AdminUser" 

Ende `

Controller: Seiten

class PagesController < ApplicationController end

Migration: Seite

`Klasse Create < Active :: Migration [5.0] def create_table ändern: Seiten tun | t |

t.integer "subject_id" 
    # same as t.references :subject 
    t.string "name" 
    t.string "permalink" 
    t.integer "position" 
    t.boolean "visible", :default => false 
    t.timestamps 

end 
add_index("pages", "subject_id") 
add_index("pages", "permalink") 

Ende end`

Antwort

1

Konsole prüfen, ist eindeutig hier ein Problem:

irb(main):003:0> section = Section.create(:name => "Section One", :position => 1) 
    (1.0ms) BEGIN 
    (1.0ms) ROLLBACK 
=> #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil> 

Wie Sie sehen können, ist ein Rollback wegen eines Validierungsfehler nach dem Abschnitt erstellen , so hat das Abschnitt Objekt eine id: nil. Ich denke, das liegt daran, dass Sie eine Validierung nicht in Ihren Fragecode aufgenommen haben, aber nachdem Sie den Abschnitt erstellt haben, können Sie einfach den Fehler in der Konsole mit section.errors überprüfen und sehen, was das Problem ist.

0

Ich wollte nur einige Informationen hinzufügen, falls jemand das gleiche Problem hat.

Wenn Lagos korrekt ist, gibt es eine Einschränkung für den Abschnitt, dem eine Seite zugewiesen sein muss. Das ist der Grund dafür, dass der Aufruf fehlschlägt.

Um das Abschnitt Objekt zu erstellen, machen Sie folgendes: (Es ist nicht im Video).

Dann erstellen Sie den Abschnitt und übergeben Sie die Seite.

section = Section.create(:name => "Section One", :position => 1, :page => page) 
0

In section.rb Datei - machen es belongs_to: Seite, {: optional => true}

dann in Schienen Konsole Run- section = Section.create (: name => " Abschnitt 1 ",: Position => 1)

Cheers

Verwandte Themen