Ruby on rails 自动保存用户和主题id
我有这个,如果我要创建一个帖子。我想自动保存用户id和主题id。但我知道它不会自动存储用户id和主题id。有人能给我解释一下原因吗?如果你能帮助我,那就更好了。:)Ruby on rails 自动保存用户和主题id,ruby-on-rails,ruby-on-rails-4,mongoid,mongoid4,Ruby On Rails,Ruby On Rails 4,Mongoid,Mongoid4,我有这个,如果我要创建一个帖子。我想自动保存用户id和主题id。但我知道它不会自动存储用户id和主题id。有人能给我解释一下原因吗?如果你能帮助我,那就更好了。:) class Post include Mongoid::Document 字段:内容,类型:字符串 属于:主题,自动保存:true 属于:用户,自动保存:true 有很多:注释,依赖::删除 嵌入很多:视频附件,与::post相反 接受\u嵌套的\u属性\u用于:视频\u附件,自动保存:true 接受\u嵌套的\u属性\u for:
class Post
include Mongoid::Document
字段:内容,类型:字符串
属于:主题,自动保存:true
属于:用户,自动保存:true
有很多:注释,依赖::删除
嵌入很多:视频附件,与::post相反
接受\u嵌套的\u属性\u用于:视频\u附件,自动保存:true
接受\u嵌套的\u属性\u for:comments,autosave:true
接受用户的嵌套属性,自动保存:true
结束
需要“mongoid/token”
班级科目
include Mongoid::Document
include Mongoid::Token
字段:标题,类型:字符串
字段:desc,类型:String
字段:讲师id,类型:字符串
字段:学生,类型:数组
有很多:用户
有很多:帖子
令牌长度:6
结束
类用户
include Mongoid::Document
字段:id\U编号,类型:字符串
字段:加密密码,类型:字符串
字段:salt,类型:String
字段:fname,类型:String
字段:lname,类型:String
字段:mname,类型:String
#关系
属于:主题,多态性:true
有一个:用户详细信息
有很多帖子吗
有很多评论
接受\u嵌套的\u属性\u for:posts,autosave:true
接受以下内容的\u嵌套\u属性\u:user\u detail,autosave:true
结束
类PostsController<应用程序控制器
def创建
@post=post.new(数据)
如果@post.save
将\u重定向到根\u url
结束
结束
私有的
def数据
参数require(:post).permit(:content,:post_by,video_attachments_attributes:[:id,:video],user_attributes:[:id],subject_attributes:[:id])
结束
无法访问end链接。能否重试。我已经公开了,你能展示你的
创建操作吗?最好在这里发布您的模型,因为读者不必打开链接来理解问题。ok create action添加了模型
class Post
include Mongoid::Document
field :content, type: String
belongs_to :subject, autosave: true
belongs_to :user , autosave: true
has_many :comments , dependent: :delete
embeds_many :video_attachments , inverse_of: :post
accepts_nested_attributes_for :video_attachments, autosave: true
accepts_nested_attributes_for :comments, autosave: true
accepts_nested_attributes_for :user, autosave: true
end
require "mongoid/token"
class Subject
include Mongoid::Document
include Mongoid::Token
field :title , type: String
field :desc , type: String
field :instructor_id , type: String
field :students , type: Array
has_many :user
has_many :post
token length: 6
end
class User
include Mongoid::Document
field :id_number , type: String
field :crypted_password , type: String
field :salt , type: String
field :fname , type: String
field :lname , type: String
field :mname , type: String
#relations
belongs_to :subject , polymorphic: true
has_one :user_detail
has_many :posts
has_many :comments
accepts_nested_attributes_for :posts , autosave: true
accepts_nested_attributes_for :user_detail , autosave: true
end
class PostsController < ApplicationController
def create
@post = Post.new(data)
if @post.save
redirect_to root_url
end
end
private
def data
params.require(:post).permit(:content, :posted_by , video_attachments_attributes: [:id , :video], user_attributes: [:id], subject_attributes: [:id])
end