ant*_*tin 5 parameters ruby-on-rails
我使用本指南作为从头开始创建消息传递系统的起点。
一切正常。但出于某种原因,每当我现在尝试通过在我的视图中单击以下链接来创建新对话时
<%= link_to 'Message me', conversations_path(sender_id: current_user.id, recipient_id: @user.id), class: 'btn btn-primary', method: :post %>
Run Code Online (Sandbox Code Playgroud)
我遇到错误:
found unpermitted parameters: _method, authenticity_token
Run Code Online (Sandbox Code Playgroud)
这里是参数:
{"_method"=>"post", "authenticity_token"=>"BL2XeA6BSjYliU2/rbdZiSnOj1N5/VMRhRIgN8LEXYPyWfxyiBM1SjYPofq7qO4+aqMhgojvnYyDyeLTcerrSQ==", "recipient_id"=>"1", "sender_id"=>"30", "controller"=>"conversations", "action"=>"create"}
Run Code Online (Sandbox Code Playgroud)
我被定向到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
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我之前没有这个问题,我也没有做任何改变。可能是什么问题?
你的参数可能应该这样定义:
def conversation_params
params.require(:conversation).permit(:sender_id, :recipient_id)
end
Run Code Online (Sandbox Code Playgroud)
这应该确保表单自动生成的其他隐藏参数不会被阻止。