Ruby on rails RubyonRails:找到未经许可的参数:\方法,真实性\u令牌

Ruby on rails RubyonRails:找到未经许可的参数:\方法,真实性\u令牌,ruby-on-rails,parameters,Ruby On Rails,Parameters,我用它作为从头开始创建消息传递系统的起点 一切都很顺利。但出于某种原因,每当我现在试图通过在我的视图中单击以下链接来创建新的对话时 <%= link_to 'Message me', conversations_path(sender_id: current_user.id, recipient_id: @user.id), class: 'btn btn-primary', method: :post %> 以下是参数: {"_method"=>"post", "authe

我用它作为从头开始创建消息传递系统的起点

一切都很顺利。但出于某种原因,每当我现在试图通过在我的视图中单击以下链接来创建新的对话时

<%= link_to 'Message me', conversations_path(sender_id: current_user.id, recipient_id: @user.id), class: 'btn btn-primary', method: :post %>
以下是参数:

{"_method"=>"post", "authenticity_token"=>"BL2XeA6BSjYliU2/rbdZiSnOj1N5/VMRhRIgN8LEXYPyWfxyiBM1SjYPofq7qO4+aqMhgojvnYyDyeLTcerrSQ==", "recipient_id"=>"1", "sender_id"=>"30", "controller"=>"conversations", "action"=>"create"}
我被引导到控制器中的
参数permit
行:

class ConversationsController < ApplicationController
  before_action :authenticate_user!

  # GET /conversations
  # GET /conversations.json
  def index
    @users = User.all

    # Restrict to conversations with at least one message and sort by last updated
    @conversations = Conversation.joins(:messages).uniq.order('updated_at DESC')
  end

  # POST /conversations
  # POST /conversations.json
  def create
    if Conversation.between(params[:sender_id], params[:recipient_id]).present?
      @conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
    else
      @conversation = Conversation.create!(conversation_params)
    end

    redirect_to conversation_messages_path(@conversation)
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def conversation_params
      params.permit(:sender_id, :recipient_id)
    end
end
类会话控制器

奇怪的是,我以前没有这个问题,我也没有做任何改变。问题可能是什么?

您的参数可能应该这样定义:

def conversation_params
  params.require(:conversation).permit(:sender_id, :recipient_id)
end

这应该确保表单自动生成的其他隐藏参数不会被阻止。

Ah,我会的!我在development.rb文件中有
config.action\u controller.action\u on\u unpermitted\u parameters=:raise
。注释掉那句话就解决了问题。这就解决了它。谢谢你的快速回复和极好的回答。添加
require
位还允许我在遇到未经许可的参数时引发错误。
def conversation_params
  params.require(:conversation).permit(:sender_id, :recipient_id)
end