Ruby on rails 中的强参数接受来自多个类的嵌套属性
它可能看起来很长,但不要害怕;) 我的对话控制器的创建功能有点问题。这是因为,使用另一款车型的带有strong参数的新Rails 4实际上可以看作是一个孩子。目前代码正在运行,但我不确定strong参数的使用是否正确。让我们看一下代码: 这是对话信息的模式Ruby on rails 中的强参数接受来自多个类的嵌套属性,ruby-on-rails,ruby-on-rails-4,strong-parameters,Ruby On Rails,Ruby On Rails 4,Strong Parameters,它可能看起来很长,但不要害怕;) 我的对话控制器的创建功能有点问题。这是因为,使用另一款车型的带有strong参数的新Rails 4实际上可以看作是一个孩子。目前代码正在运行,但我不确定strong参数的使用是否正确。让我们看一下代码: 这是对话信息的模式 class ConversationMessage < ActiveRecord::Base # def change # create_table :conversation_messages do |t|
class ConversationMessage < ActiveRecord::Base
# def change
# create_table :conversation_messages do |t|
# t.integer :message_id
# t.integer :conversation_id
# end
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
end
class MessageGroupMessage < ActiveRecord::Base
# def change
# create_table :message_group_messages do |t|
# t.integer :user_id
# t.integer :message_id
# t.integer :message_group_id
# end
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :message_group, class_name: 'MessageGroup', foreign_key: :message_group_id
end
class MessageGroup < ActiveRecord::Base
# def change
# create_table :message_groups do |t|
# t.integer :id_user
# t.string :message_group_name
# t.timestamps
# end
# add_index :message_groups, :id_user
# end
belongs_to :user, class_name: 'User', foreign_key: :id_user
has_and_belongs_to_many :messages, :join_table => "message_group_messages", class_name: "Message"
has_many :message_group_messages, class_name: "MessageGroupMessage"
end
class BodyMessage < ActiveRecord::Base
# def change
# create_table :body_messages do |t|
# t.integer :message_id
# t.text :body
# t.timestamps
# end
# add_index :body_messages, :message_id
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
end
class Message < ActiveRecord::Base
# def change
# create_table :messages do |t|
# t.integer :id_sender
# t.integer :id_receiver
# t.timestamps
# end
# end
belongs_to :sender, class_name: 'User', foreign_key: :id_sender
belongs_to :receiver, class_name: 'User', foreign_key: :id_receiver
has_one :conversation_message, class_name: "ConversationMessage"
has_one :conversation, through: :conversation_message, source: :conversation
has_and_belongs_to_many :message_groups, :join_table => "message_group_messages", class_name: "MessageGroup"
has_many :message_group_messages, class_name: "MessageGroupMessage"
has_one :body_message, class_name: "BodyMessage"
end
class FoldersConversation < ActiveRecord::Base
# def change
# create_table :folders_conversations do |t|
# t.integer :conversations_folder_id
# t.integer :conversation_id
# end
# end
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
belongs_to :conversations_folder, class_name: 'ConversationsFolder', foreign_key: :conversations_folder_id
end
class ConversationsFolder < ActiveRecord::Base
# def change
# create_table :conversations_folders do |t|
# t.integer :user_id
# t.string :folder_name
# t.timestamps
# end
# add_index :conversations_folders, :user_id
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
has_and_belongs_to_many :conversations, :join_table => "folders_conversations", class_name: "Conversation"
has_many :folders_conversations, class_name: 'FoldersConversation'
end
class ConversationMessage
这是MessageGroupMessage模型
class ConversationMessage < ActiveRecord::Base
# def change
# create_table :conversation_messages do |t|
# t.integer :message_id
# t.integer :conversation_id
# end
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
end
class MessageGroupMessage < ActiveRecord::Base
# def change
# create_table :message_group_messages do |t|
# t.integer :user_id
# t.integer :message_id
# t.integer :message_group_id
# end
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :message_group, class_name: 'MessageGroup', foreign_key: :message_group_id
end
class MessageGroup < ActiveRecord::Base
# def change
# create_table :message_groups do |t|
# t.integer :id_user
# t.string :message_group_name
# t.timestamps
# end
# add_index :message_groups, :id_user
# end
belongs_to :user, class_name: 'User', foreign_key: :id_user
has_and_belongs_to_many :messages, :join_table => "message_group_messages", class_name: "Message"
has_many :message_group_messages, class_name: "MessageGroupMessage"
end
class BodyMessage < ActiveRecord::Base
# def change
# create_table :body_messages do |t|
# t.integer :message_id
# t.text :body
# t.timestamps
# end
# add_index :body_messages, :message_id
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
end
class Message < ActiveRecord::Base
# def change
# create_table :messages do |t|
# t.integer :id_sender
# t.integer :id_receiver
# t.timestamps
# end
# end
belongs_to :sender, class_name: 'User', foreign_key: :id_sender
belongs_to :receiver, class_name: 'User', foreign_key: :id_receiver
has_one :conversation_message, class_name: "ConversationMessage"
has_one :conversation, through: :conversation_message, source: :conversation
has_and_belongs_to_many :message_groups, :join_table => "message_group_messages", class_name: "MessageGroup"
has_many :message_group_messages, class_name: "MessageGroupMessage"
has_one :body_message, class_name: "BodyMessage"
end
class FoldersConversation < ActiveRecord::Base
# def change
# create_table :folders_conversations do |t|
# t.integer :conversations_folder_id
# t.integer :conversation_id
# end
# end
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
belongs_to :conversations_folder, class_name: 'ConversationsFolder', foreign_key: :conversations_folder_id
end
class ConversationsFolder < ActiveRecord::Base
# def change
# create_table :conversations_folders do |t|
# t.integer :user_id
# t.string :folder_name
# t.timestamps
# end
# add_index :conversations_folders, :user_id
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
has_and_belongs_to_many :conversations, :join_table => "folders_conversations", class_name: "Conversation"
has_many :folders_conversations, class_name: 'FoldersConversation'
end
class MessageGroupMessage
这是消息组模型
class ConversationMessage < ActiveRecord::Base
# def change
# create_table :conversation_messages do |t|
# t.integer :message_id
# t.integer :conversation_id
# end
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
end
class MessageGroupMessage < ActiveRecord::Base
# def change
# create_table :message_group_messages do |t|
# t.integer :user_id
# t.integer :message_id
# t.integer :message_group_id
# end
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :message_group, class_name: 'MessageGroup', foreign_key: :message_group_id
end
class MessageGroup < ActiveRecord::Base
# def change
# create_table :message_groups do |t|
# t.integer :id_user
# t.string :message_group_name
# t.timestamps
# end
# add_index :message_groups, :id_user
# end
belongs_to :user, class_name: 'User', foreign_key: :id_user
has_and_belongs_to_many :messages, :join_table => "message_group_messages", class_name: "Message"
has_many :message_group_messages, class_name: "MessageGroupMessage"
end
class BodyMessage < ActiveRecord::Base
# def change
# create_table :body_messages do |t|
# t.integer :message_id
# t.text :body
# t.timestamps
# end
# add_index :body_messages, :message_id
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
end
class Message < ActiveRecord::Base
# def change
# create_table :messages do |t|
# t.integer :id_sender
# t.integer :id_receiver
# t.timestamps
# end
# end
belongs_to :sender, class_name: 'User', foreign_key: :id_sender
belongs_to :receiver, class_name: 'User', foreign_key: :id_receiver
has_one :conversation_message, class_name: "ConversationMessage"
has_one :conversation, through: :conversation_message, source: :conversation
has_and_belongs_to_many :message_groups, :join_table => "message_group_messages", class_name: "MessageGroup"
has_many :message_group_messages, class_name: "MessageGroupMessage"
has_one :body_message, class_name: "BodyMessage"
end
class FoldersConversation < ActiveRecord::Base
# def change
# create_table :folders_conversations do |t|
# t.integer :conversations_folder_id
# t.integer :conversation_id
# end
# end
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
belongs_to :conversations_folder, class_name: 'ConversationsFolder', foreign_key: :conversations_folder_id
end
class ConversationsFolder < ActiveRecord::Base
# def change
# create_table :conversations_folders do |t|
# t.integer :user_id
# t.string :folder_name
# t.timestamps
# end
# add_index :conversations_folders, :user_id
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
has_and_belongs_to_many :conversations, :join_table => "folders_conversations", class_name: "Conversation"
has_many :folders_conversations, class_name: 'FoldersConversation'
end
class MessageGroup“message_group_messages”,class_name:“message”
有很多:消息组消息,类名称:“MessageGroupMessage”
结束
这是BodyMessage型号
class ConversationMessage < ActiveRecord::Base
# def change
# create_table :conversation_messages do |t|
# t.integer :message_id
# t.integer :conversation_id
# end
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
end
class MessageGroupMessage < ActiveRecord::Base
# def change
# create_table :message_group_messages do |t|
# t.integer :user_id
# t.integer :message_id
# t.integer :message_group_id
# end
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :message_group, class_name: 'MessageGroup', foreign_key: :message_group_id
end
class MessageGroup < ActiveRecord::Base
# def change
# create_table :message_groups do |t|
# t.integer :id_user
# t.string :message_group_name
# t.timestamps
# end
# add_index :message_groups, :id_user
# end
belongs_to :user, class_name: 'User', foreign_key: :id_user
has_and_belongs_to_many :messages, :join_table => "message_group_messages", class_name: "Message"
has_many :message_group_messages, class_name: "MessageGroupMessage"
end
class BodyMessage < ActiveRecord::Base
# def change
# create_table :body_messages do |t|
# t.integer :message_id
# t.text :body
# t.timestamps
# end
# add_index :body_messages, :message_id
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
end
class Message < ActiveRecord::Base
# def change
# create_table :messages do |t|
# t.integer :id_sender
# t.integer :id_receiver
# t.timestamps
# end
# end
belongs_to :sender, class_name: 'User', foreign_key: :id_sender
belongs_to :receiver, class_name: 'User', foreign_key: :id_receiver
has_one :conversation_message, class_name: "ConversationMessage"
has_one :conversation, through: :conversation_message, source: :conversation
has_and_belongs_to_many :message_groups, :join_table => "message_group_messages", class_name: "MessageGroup"
has_many :message_group_messages, class_name: "MessageGroupMessage"
has_one :body_message, class_name: "BodyMessage"
end
class FoldersConversation < ActiveRecord::Base
# def change
# create_table :folders_conversations do |t|
# t.integer :conversations_folder_id
# t.integer :conversation_id
# end
# end
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
belongs_to :conversations_folder, class_name: 'ConversationsFolder', foreign_key: :conversations_folder_id
end
class ConversationsFolder < ActiveRecord::Base
# def change
# create_table :conversations_folders do |t|
# t.integer :user_id
# t.string :folder_name
# t.timestamps
# end
# add_index :conversations_folders, :user_id
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
has_and_belongs_to_many :conversations, :join_table => "folders_conversations", class_name: "Conversation"
has_many :folders_conversations, class_name: 'FoldersConversation'
end
class BodyMessage
下面是消息型号
class ConversationMessage < ActiveRecord::Base
# def change
# create_table :conversation_messages do |t|
# t.integer :message_id
# t.integer :conversation_id
# end
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
end
class MessageGroupMessage < ActiveRecord::Base
# def change
# create_table :message_group_messages do |t|
# t.integer :user_id
# t.integer :message_id
# t.integer :message_group_id
# end
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :message_group, class_name: 'MessageGroup', foreign_key: :message_group_id
end
class MessageGroup < ActiveRecord::Base
# def change
# create_table :message_groups do |t|
# t.integer :id_user
# t.string :message_group_name
# t.timestamps
# end
# add_index :message_groups, :id_user
# end
belongs_to :user, class_name: 'User', foreign_key: :id_user
has_and_belongs_to_many :messages, :join_table => "message_group_messages", class_name: "Message"
has_many :message_group_messages, class_name: "MessageGroupMessage"
end
class BodyMessage < ActiveRecord::Base
# def change
# create_table :body_messages do |t|
# t.integer :message_id
# t.text :body
# t.timestamps
# end
# add_index :body_messages, :message_id
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
end
class Message < ActiveRecord::Base
# def change
# create_table :messages do |t|
# t.integer :id_sender
# t.integer :id_receiver
# t.timestamps
# end
# end
belongs_to :sender, class_name: 'User', foreign_key: :id_sender
belongs_to :receiver, class_name: 'User', foreign_key: :id_receiver
has_one :conversation_message, class_name: "ConversationMessage"
has_one :conversation, through: :conversation_message, source: :conversation
has_and_belongs_to_many :message_groups, :join_table => "message_group_messages", class_name: "MessageGroup"
has_many :message_group_messages, class_name: "MessageGroupMessage"
has_one :body_message, class_name: "BodyMessage"
end
class FoldersConversation < ActiveRecord::Base
# def change
# create_table :folders_conversations do |t|
# t.integer :conversations_folder_id
# t.integer :conversation_id
# end
# end
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
belongs_to :conversations_folder, class_name: 'ConversationsFolder', foreign_key: :conversations_folder_id
end
class ConversationsFolder < ActiveRecord::Base
# def change
# create_table :conversations_folders do |t|
# t.integer :user_id
# t.string :folder_name
# t.timestamps
# end
# add_index :conversations_folders, :user_id
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
has_and_belongs_to_many :conversations, :join_table => "folders_conversations", class_name: "Conversation"
has_many :folders_conversations, class_name: 'FoldersConversation'
end
class消息“消息组”消息,类名称:“消息组”
有很多:消息组消息,类名称:“MessageGroupMessage”
有一个:body消息,类名称:“BodyMessage”
结束
这是翻页模型
class ConversationMessage < ActiveRecord::Base
# def change
# create_table :conversation_messages do |t|
# t.integer :message_id
# t.integer :conversation_id
# end
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
end
class MessageGroupMessage < ActiveRecord::Base
# def change
# create_table :message_group_messages do |t|
# t.integer :user_id
# t.integer :message_id
# t.integer :message_group_id
# end
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :message_group, class_name: 'MessageGroup', foreign_key: :message_group_id
end
class MessageGroup < ActiveRecord::Base
# def change
# create_table :message_groups do |t|
# t.integer :id_user
# t.string :message_group_name
# t.timestamps
# end
# add_index :message_groups, :id_user
# end
belongs_to :user, class_name: 'User', foreign_key: :id_user
has_and_belongs_to_many :messages, :join_table => "message_group_messages", class_name: "Message"
has_many :message_group_messages, class_name: "MessageGroupMessage"
end
class BodyMessage < ActiveRecord::Base
# def change
# create_table :body_messages do |t|
# t.integer :message_id
# t.text :body
# t.timestamps
# end
# add_index :body_messages, :message_id
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
end
class Message < ActiveRecord::Base
# def change
# create_table :messages do |t|
# t.integer :id_sender
# t.integer :id_receiver
# t.timestamps
# end
# end
belongs_to :sender, class_name: 'User', foreign_key: :id_sender
belongs_to :receiver, class_name: 'User', foreign_key: :id_receiver
has_one :conversation_message, class_name: "ConversationMessage"
has_one :conversation, through: :conversation_message, source: :conversation
has_and_belongs_to_many :message_groups, :join_table => "message_group_messages", class_name: "MessageGroup"
has_many :message_group_messages, class_name: "MessageGroupMessage"
has_one :body_message, class_name: "BodyMessage"
end
class FoldersConversation < ActiveRecord::Base
# def change
# create_table :folders_conversations do |t|
# t.integer :conversations_folder_id
# t.integer :conversation_id
# end
# end
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
belongs_to :conversations_folder, class_name: 'ConversationsFolder', foreign_key: :conversations_folder_id
end
class ConversationsFolder < ActiveRecord::Base
# def change
# create_table :conversations_folders do |t|
# t.integer :user_id
# t.string :folder_name
# t.timestamps
# end
# add_index :conversations_folders, :user_id
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
has_and_belongs_to_many :conversations, :join_table => "folders_conversations", class_name: "Conversation"
has_many :folders_conversations, class_name: 'FoldersConversation'
end
class FoldersConversation
这是ConversationsFolder的模型
class ConversationMessage < ActiveRecord::Base
# def change
# create_table :conversation_messages do |t|
# t.integer :message_id
# t.integer :conversation_id
# end
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
end
class MessageGroupMessage < ActiveRecord::Base
# def change
# create_table :message_group_messages do |t|
# t.integer :user_id
# t.integer :message_id
# t.integer :message_group_id
# end
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
belongs_to :message, class_name: 'Message', foreign_key: :message_id
belongs_to :message_group, class_name: 'MessageGroup', foreign_key: :message_group_id
end
class MessageGroup < ActiveRecord::Base
# def change
# create_table :message_groups do |t|
# t.integer :id_user
# t.string :message_group_name
# t.timestamps
# end
# add_index :message_groups, :id_user
# end
belongs_to :user, class_name: 'User', foreign_key: :id_user
has_and_belongs_to_many :messages, :join_table => "message_group_messages", class_name: "Message"
has_many :message_group_messages, class_name: "MessageGroupMessage"
end
class BodyMessage < ActiveRecord::Base
# def change
# create_table :body_messages do |t|
# t.integer :message_id
# t.text :body
# t.timestamps
# end
# add_index :body_messages, :message_id
# end
belongs_to :message, class_name: 'Message', foreign_key: :message_id
end
class Message < ActiveRecord::Base
# def change
# create_table :messages do |t|
# t.integer :id_sender
# t.integer :id_receiver
# t.timestamps
# end
# end
belongs_to :sender, class_name: 'User', foreign_key: :id_sender
belongs_to :receiver, class_name: 'User', foreign_key: :id_receiver
has_one :conversation_message, class_name: "ConversationMessage"
has_one :conversation, through: :conversation_message, source: :conversation
has_and_belongs_to_many :message_groups, :join_table => "message_group_messages", class_name: "MessageGroup"
has_many :message_group_messages, class_name: "MessageGroupMessage"
has_one :body_message, class_name: "BodyMessage"
end
class FoldersConversation < ActiveRecord::Base
# def change
# create_table :folders_conversations do |t|
# t.integer :conversations_folder_id
# t.integer :conversation_id
# end
# end
belongs_to :conversation, class_name: 'Conversation', foreign_key: :conversation_id
belongs_to :conversations_folder, class_name: 'ConversationsFolder', foreign_key: :conversations_folder_id
end
class ConversationsFolder < ActiveRecord::Base
# def change
# create_table :conversations_folders do |t|
# t.integer :user_id
# t.string :folder_name
# t.timestamps
# end
# add_index :conversations_folders, :user_id
# end
belongs_to :user, class_name: 'User', foreign_key: :user_id
has_and_belongs_to_many :conversations, :join_table => "folders_conversations", class_name: "Conversation"
has_many :folders_conversations, class_name: 'FoldersConversation'
end
class ConversationsFolder“文件夹”\u对话,类别名称:“对话”
有很多:文件夹对话,类名:'FoldersConversation'
结束
这是对话模型,其中的视图将成为所有其他类的“childs”
类对话“文件夹对话”,类别名称:“对话文件夹”
有很多:文件夹对话,类名:'FoldersConversation'
有很多:消息组消息,通过::消息,源::消息组消息
有许多:消息组,通过::消息,源::消息组
has_和_属于_many:messages,:join_table=>“conversation_messages”,class_name:“Message”
接受\u嵌套的\u属性\u for:messages,允许\u destroy:true
有很多:body\u消息、through::messages、source::body\u消息
接受\u嵌套的\u属性\u用于:正文\u消息,允许\u销毁:true
有很多:会话消息,类名称:“会话消息”
接受\u嵌套的\u属性\u用于:对话\u消息,允许\u销毁:true
结束
这里是对话控制器
class ConversationsController < ApplicationController
def create
### GET FROM VIEW -> HERE WHERE THE STRONG PARAMS SHOULD BE WORKING FOR: subject AND body
@friend = User.friendly.find(params[:id_friend])
@subject = params[:conversation][:conversation_attributes][:subject]
@body = params[:conversation][:body_messages]
@conversation_new = Conversation.new(conversation_params)
@conversation = @conversation_new.create_new_conversation(current_user, @friend, @subject)
@message_new = Message.new(message_params)
@message = @message_new.message_new(current_user, @friend)
@body_message_new = BodyMessage.new(body_message_params)
@body_message = @body_message_new.body_message_new(@message, @body)
@conversation_message_new = ConversationMessage.new(conversation_message_params)
@conversation_message = @conversation_message_new.conversation_message_new(@conversation, @message)
folders_conversation = @conversation.add_new_to_folders_conversation(current_user,@friend)
respond_to do |format|
format.html { redirect_to conversations_url }
format.js
end
end
private
###
### HERE ARE THE STRONG PARAMS
###
def conversation_params
params.require(:conversation).permit(:subject)
end
### THESE ARE THE ONES THAT ARE WORKING BUT FOR SURE NOT BY THE CORRECT WAY
def message_params
params.require(:conversation).permit(messages_attributes: [:id_receiver])
end
def body_message_params
params.require(:conversation).permit(body_messages_attributes: [:message_id, :body])
end
def conversation_message_params
params.require(:conversation).permit(conversation_messages_attributes: [:message_id])
end
end
end
类会话控制器这里是强参数应该用于的位置:主题和主体
@friend=User.friendly.find(参数[:id\u f