2015-10-30 45 views
0

这是一个后续此主题:Ruby on Rails: found unpermitted parameters: _method, authenticity_token参数是丢失或为空值:谈话

我有以下行:<%= button_to 'Message me', conversations_path(sender_id: current_user.id, recipient_id: @user.id), class: 'btn btn-primary m-t' %>

但是当我按一下按钮,我得到的错误: param is missing or the value is empty: conversation

我可以看到,conversation是不是在params哈希表:{"authenticity_token"=>"r5pwStXl6NwEgqqq0GT0RQxCqsGHrTVsh4Q7HviX+re5k+XOs2ioRv9kZqvDGz9Ch/6O6D1nOMjscquHQJlB+g==", "recipient_id"=>"1", "sender_id"=>"2", "controller"=>"conversations", "action"=>"create"}

正如其他线程建议,这是有益的补充require(:conversation)控制器:

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.require(:conversation).permit(:sender_id, :recipient_id) 
    end 
end 

这工作了一段时间,但由于某种原因停止工作。我应该如何解决这个问题?为什么它停止工作?

回答

0

手动添加“对话”到哈希似乎工作:<%= button_to 'Message me', conversations_path(conversation: { sender_id: current_user.id, recipient_id: @user.id }), class: 'btn btn-primary m-t' %>

我也不得不修复控制器考虑到嵌套:

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

    redirect_to conversation_messages_path(@conversation) 
end 
相关问题