简体   繁体   English

Rails-模型未保存到数据库

[英]Rails - Model not saving to db

I'm completing this airbnb clone course ( https://code4startup.com/projects/build-airbnb-with-ruby-on-rails-level-1 ) but have diverted a bit in order to complete my own project; 我正在完成这个airbnb克隆课程( https://code4startup.com/projects/build-airbnb-with-ruby-on-rails-level-1 ),但为了完成我自己的项目做了一些改动。 a marketplace for education camps. 教育营市场。 Therefore I've added an additional model 'Courses'. 因此,我添加了一个附加模型“课程”。 It now has User>Listing>Course. 现在,它具有用户>列表>课程。 This Courses model is working in rails console but not saving to my database when I'm running the server. 此课程模型在Rails控制台中运行,但在运行服务器时无法保存到数据库中。 Any suggestions would be appreciated... 任何建议,将不胜感激...

Error Message 错误信息

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

Models 楷模

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

Routes 路线

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

below you can read my comments with the # sign 在下面,您可以阅读带有#号的评论

You are trying to save an object Course that has belongs_to listings so it is expected that it has as course.listing_id the id of an existing Listing 您试图保存对象Coursebelongs_to listings ,因此预计它作为course.listing_idid现有的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM