2017-10-23 2 views
0

Ich vervollständige diesen Airbnb Klon Kurs (https://code4startup.com/projects/build-airbnb-with-ruby-on-rails-level-1) aber habe etwas umgeleitet, um mein eigenes Projekt abzuschließen; ein Marktplatz für Bildungscamps. Daher habe ich ein zusätzliches Modell 'Kurse' hinzugefügt. Es hat jetzt Benutzer> Listing> Kurs. Dieses Kursmodell funktioniert in der Rails-Konsole, wird aber nicht in meiner Datenbank gespeichert, wenn ich den Server aktiviere. Alle Vorschläge würden geschätzt ...Schienen - Modell speichert nicht in db

Fehlermeldung

ActiveRecord::RecordInvalid in CoursesController#create 
Validation failed: Listing must exist 

Modelle

class User < ApplicationRecord 
    has_many :listings 
    has_many :courses, :through => :listings 
end 

class Listing < ApplicationRecord 
    belongs_to :user 
    has_many :courses 

    validates :listing_type, presence: true 
    validates :course_type, presence: true 
    validates :accommodate, presence: true 
end 

class Course < ApplicationRecord 
    belongs_to :listing 

    validates :curriculum_type, presence: true 
    validates :course_places, presence: true 
end 

Course-Controller

class CoursesController < ApplicationController 

    before_action :set_course, except: [:index, :new, :create] 
    before_action :authenticate_user!, except: [:show] 

    def index 
    @courses = current_user.courses 
    end 

    def new 
    @course = current_user.courses.build 
    end 

    def create 
    @course = current_user.courses.build(course_params) 
    if @course.save! 
     redirect_to course_listing_path(@course), notice: "Saved..." 
    else 
     render :new, notice: "Something went wrong..." 
    end 
    end 

    def show 
    end 


    def listing 
    end 

    def pricing 
    end 

    def description 
    end 

    def photo_upload 
    end 

    def amenities 
    end 

    def location 
    end 

    def update 
    if @course.update(course_params) 
     flash[:notice] = "Saved..." 
    else 
     flash[:notice] = "Something went wrong..." 
    end 
    redirect_back(fallback_location: request.referer) 
    end 

    private 

    def set_course 
    @course = Course.find(params[:id]) 
    end 

    def course_params 
    params.require(:course).permit(:name, :curriculum_type, :summary, :address, :course_places, :start_date, :finish_date, :price) 
    end 

end 

Routen

Rails.application.routes.draw do 

    root 'pages#home' 

    devise_for :users, 
       path: '', 
       path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'}, 
       controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' } 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 

    resources :users, only: [:show] 
    resources :listings, except: [:edit] do 
    member do 
     get 'listing' 
     get 'pricing' 
     get 'description' 
     get 'photo_upload' 
     get 'amenities' 
     get 'location' 
    end 
    end 

    resources :courses, except: [:edit] do 
    member do 
     get 'listing' 
     get 'pricing' 
     get 'description' 
     get 'photo_upload' 
     get 'amenities' 
     get 'location' 
    end 
    end 


end 

Antwort

1

unter Ihnen meine Ausführungen mit dem # Zeichen

Sie versuchen, ein Objekt zu speichern lesen kann Course dass belongs_to listings so ist es zu erwarten, dass es als course.listing_id hat die id eines bestehenden Listing

def create 
    @course = current_user.courses.build(course_params) 
    # You need to set @course.listing_id to an existing Listing 
    # You need to find that listing and save it in a variable. 
    # I am not getting into your logic, because your code is confused and need many adjustments 

    listing = Listing.find() # include hear your logic to find an existing listing from the db 
    @course.listing_id = listing.id 
    if @course.save! 
     redirect_to course_listing_path(@course), notice: "Saved..." 
    else 
     render :new, notice: "Something went wrong..." 
    end 
end 
+0

Das ist große Hilfe, danke. Für die "Listing = Listing.find()" was wäre der am weitesten verbreitete Weg? Oder gibt es eine Möglichkeit, dies automatisch zu tun, z. Klicken Sie auf die Schaltfläche Neuer Kurs auf der Seite Listing? –

+0

@JackRiminton wenn es eine Beziehung zwischen 2 Modellen gibt, und ein Elternteil des anderen ist, werde ich versuchen, verschachtelte Ressourcen http://guides.rubyonrails.org/routing.html#nested-resources zu verwenden und die ID zu übergeben 'als Parameter in meiner 'URL' http' Post' Anfrage an den Server, so sollte die Post-Anfrage aussehen wie '/ listings /: listing_id/Kurse' und es wird von der Aktion' courses # create' durchgeführt, auf diese Weise kann tun 'Listing.find (params [: listing_id])' und Ihre Ressourcen sind 'Ressourcen: Einträge tun Ressourcen: Kurse Ende' –

+0

Ok danke, ich werde gehen und studieren, dass –

Verwandte Themen