我以这篇指南作为从零开始创建消息系统的起点。
一切都运行良好。但出现了一个问题,每当我尝试通过点击视图中的以下链接创建新对话时。
我遇到了这个错误:
以下是参数列表:
一切都运行良好。但出现了一个问题,每当我尝试通过点击视图中的以下链接创建新对话时。
<%= link_to 'Message me', conversations_path(sender_id: current_user.id, recipient_id: @user.id), class: 'btn btn-primary', method: :post %>
我遇到了这个错误:
found unpermitted parameters: _method, authenticity_token
以下是参数列表:
{"_method"=>"post", "authenticity_token"=>"BL2XeA6BSjYliU2/rbdZiSnOj1N5/VMRhRIgN8LEXYPyWfxyiBM1SjYPofq7qO4+aqMhgojvnYyDyeLTcerrSQ==", "recipient_id"=>"1", "sender_id"=>"30", "controller"=>"conversations", "action"=>"create"}
我被指示关注于我的控制器中的params.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
奇怪的是,我以前没有遇到过这个问题,也没有做任何更改。可能出了什么问题?
config.action_controller.action_on_unpermitted_parameters = :raise
。注释掉这行代码就可以解决问题了。 - anti-destin