cho*_*s24 3 ruby ruby-on-rails ruby-on-rails-5
我不明白如何工作optional: true。它已经解决了必须存在的错误

我\xe2\x80\x99m通过rails安装了以下功能,由于验证,记录没有保存。\n我听说optional: true允许保存nil记录,但没有optional: true记录就没有保存'不过没有。当我尝试访问时发生错误relation#create。
我的代码在这里 \xe2\x86\x93
\nclass User < ApplicationRecord\n # Include default devise modules. Others available are:\n # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable\n devise :database_authenticatable, :registerable,\n :recoverable, :rememberable, :validatable\n \n has_many :articles\n has_many :goods\n \n has_many :active_relations, class_name: "Relation", foreign_key: :following_id\n has_many :passive_rerations, class_name: "Relation", foreign_key: :follower_id\n \n has_many :followings, through: :active_relations, source: :follower\n has_many :followers, through: :passive_rerations, source: :following\nend\nRun Code Online (Sandbox Code Playgroud)\nclass Relation < ApplicationRecord\n belongs_to :followings, class_name: "User", optional: true\n belongs_to :followers, class_name: "User", optional: true\nend\nRun Code Online (Sandbox Code Playgroud)\nclass RelationsController < ApplicationController\n def create\n follow = current_user.active_relations.new(follower_id: params[:user_id])\n follow.save!\n redirect_to root_path\n end\nend\nRun Code Online (Sandbox Code Playgroud)\nRails.application.routes.draw do\n devise_for :user\n # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html\n root "articles#index"\n resources :articles, except: [:edit, :delete] do\n resources :goods, only: [:create, :destroy]\n get "gooder", on: :member\n end\n resources :users, only: :show do\n resources :relations, only: [:create, :destroy]\n get "followings", on: :member\n get "followers", on: :member\n end\nend\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
8044 次 |
| 最近记录: |