Ruby on Rails: found unpermitted parameters: _method, authenticity_token

蓝咒 提交于 2019-12-10 17:00:48

问题


I used this guide as a starting point for creating a messaging system from scratch.

Everything worked fine. But for some reason, whenever I now try to create a new conversation by clicking in my view the following link

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

I encounter the error:

found unpermitted parameters: _method, authenticity_token

Here are the params:

{"_method"=>"post", "authenticity_token"=>"BL2XeA6BSjYliU2/rbdZiSnOj1N5/VMRhRIgN8LEXYPyWfxyiBM1SjYPofq7qO4+aqMhgojvnYyDyeLTcerrSQ==", "recipient_id"=>"1", "sender_id"=>"30", "controller"=>"conversations", "action"=>"create"}

I am directed to the params.permit line in my controller:

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

Strangely, I did not have this issue before, and I have not made any changes. What might the issue be?


回答1:


Your params should probably be defined like this:

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

This should make sure that the other hidden parameters that are generated by the form automatically are not being blocked.



来源:https://stackoverflow.com/questions/33089902/ruby-on-rails-found-unpermitted-parameters-method-authenticity-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!