Ruby on rails Rails ActionController::ParameterMissing(param缺失或值为空

Ruby on rails Rails ActionController::ParameterMissing(param缺失或值为空,ruby-on-rails,ruby,Ruby On Rails,Ruby,我使用指南作为从头开始创建消息传递系统的起点。我必须修改这些指南来处理User和AdminUser之间的消息。出于某种原因,每当我现在尝试通过在视图中单击以下链接来创建新对话时: <li><%= link_to admin.email, conversations_path(sendable_id: current_user.id, recipientable_id: admin.id), method: :post %></li> 模式: create_t

我使用指南作为从头开始创建消息传递系统的起点。我必须修改这些指南来处理
User
AdminUser
之间的消息。出于某种原因,每当我现在尝试通过在视图中单击以下链接来创建新对话时:

<li><%= link_to admin.email, conversations_path(sendable_id: current_user.id, recipientable_id: admin.id), method: :post %></li>
模式:

create_table "conversations", force: :cascade do |t|
    t.string "sendable_type"
    t.bigint "sendable_id"
    t.string "recipientable_type"
    t.bigint "recipientable_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["recipientable_type", "recipientable_id"], name: "index_conversations_on_recipientable"
    t.index ["sendable_type", "sendable_id"], name: "index_conversations_on_sendable"
  end

您需要帮助Rails了解您引用的多态模型;如果您只提供ID,则会失败,因为Rails还需要多态类型(请记住:该类型是必需的,因此Rails可以链接到实际表。在您的情况下,有两种可能的类型User和AdminUser)

只需提供多态类型,并将它们添加到
conversation\u params
方法中。通过查看您的代码,我猜这就是您想要的:

<li><%= link_to admin.email, conversations_path(sendable_id: current_user.id, sendable_type: 'User', recipientable_id: admin.id, recipientable_type: 'AdminUser'), method: :post %></li>

  • 第一个错误是显而易见的,对吗?要么使用带有适当对话对象的真实表单,要么像以前那样将其从所需参数中删除。第二个错误更难理解-你确定你有一个可发送和可接收的ID吗?它是一个多态表(如果是,你可能缺少polymorhpic类型?)@JoelBlum I在更新的question@JoelBlum我们的想法是在
    用户
    管理员
    class User < ApplicationRecord
      has_many :conversations, as: :sendable
      has_many :conversations, as: :recipientable
    end
    
    
    class AdminUser < ApplicationRecord
      has_many :conversations, as: :sendable
      has_many :conversations, as: :recipientable
    end
    
    class Conversation < ApplicationRecord
      belongs_to :sendable, polymorphic: true
      belongs_to :recipientable, polymorphic: true
      has_many :messages, dependent: :destroy
    
      validates :sendable_id, uniqueness: { scope: :recipientable_id }
    end
    
    class Message < ApplicationRecord
      belongs_to :conversation
      belongs_to :messageable, polymorphic: true
    
      validates_presence_of :body
    end
    
    create_table "conversations", force: :cascade do |t|
        t.string "sendable_type"
        t.bigint "sendable_id"
        t.string "recipientable_type"
        t.bigint "recipientable_id"
        t.datetime "created_at", precision: 6, null: false
        t.datetime "updated_at", precision: 6, null: false
        t.index ["recipientable_type", "recipientable_id"], name: "index_conversations_on_recipientable"
        t.index ["sendable_type", "sendable_id"], name: "index_conversations_on_sendable"
      end
    
    <li><%= link_to admin.email, conversations_path(sendable_id: current_user.id, sendable_type: 'User', recipientable_id: admin.id, recipientable_type: 'AdminUser'), method: :post %></li>