Rails - 模型不保存到db

时间:2017-10-23 12:54:58

标签: ruby-on-rails ruby database validation activerecord

我正在完成这个airbnb克隆课程(https://code4startup.com/projects/build-airbnb-with-ruby-on-rails-level-1),但为了完成我自己的项目而转移了一点;教育营地的市场。因此,我添加了一个额外的模型'课程'。它现在有User> Listing> Course。这个课程模型在rails控制台中工作,但在我运行服务器时没有保存到我的数据库。任何建议将不胜感激......

错误消息

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

模型

    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

课程控制器

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

路线

((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level = Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);

1 个答案:

答案 0 :(得分:1)

您可以在下面的<{1}}符号

中阅读我的评论

您正在尝试保存具有#的对象Course,因此预计其belongs_to listings course.listing_id为现有id < / p>

Listing