【问题标题】:ArgumentError in Messages#index while building conversation + messages model构建对话+消息模型时消息中的参数错误#index
【发布时间】:2016-10-24 23:59:13
【问题描述】:

对于我正在开发的市场,我想在用户之间添加消息传递功能。

我发现 this tutorial 符合我的要求,我正在尝试复制它。

但是,当尝试向其他用户发送消息时,我收到以下错误:

消息中的参数错误#index

显示第 9 行出现的 /home/ubuntu/workspace/marketplace/app/views/messages/index.html.erb:

表单中的第一个参数不能包含 nil 或为空

从与之相关的消息视图文件中:

<%= form_for [@conversation, @message] do |f| %>
<%= f.text_area :body %>
<%= f.text_field :user_id, value: current_user.id %>
<%= f.submit "Add Reply" %>

创建了一个 conversation_id,但在消息视图文件中出错了。

有人知道我错过了什么吗?我对 Rails 很陌生,现在我已经在这上面瞎了两晚。非常感谢您的帮助!

Routes.rb

resources :conversations do
    resources :messages
end

对话模型:

class Conversation < ApplicationRecord
  belongs_to :sender, :foreign_key => :sender_id, class_name: "User"
  belongs_to :recipient, :foreign_key => :recipient_id, class_name: "User"
  
  has_many :messages, dependent: :destroy
  
  validates_uniqueness_of :sender_id, :scope => :recipient_id

  scope :between, -> (sender_id, recipient_id) do
    where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", sender_id, recipient_id, recipient_id, sender_id)
  end

end

消息模型:

class Message < ApplicationRecord
  belongs_to :conversation
  belongs_to :user

  validates_presence_of :body, :conversation_id, :user_id
  
  def message_time
    created_at.strftime("%_d %b %Y at %k:%M")
  end

end

对话控制器:

class ConversationsController < ApplicationController
 before_action :authenticate_user!

 def index
  @users = User.all
  @conversations = Conversation.all
 end

 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
   
 def conversation_params
  params.permit(:sender_id, :recipient_id)
 end

end

消息控制器:

class MessagesController < ApplicationController
  before_action do
    @conversation = Conversation.find(params[:conversation_id])
  end

  def index
    @messages = @conversation.messages
  end
  
  def new
    @message = @conversation.messages.new
    
  end
  
  def create
    @message = @conversation.messages.new(message_params)
    if @message.save
      redirect_to conversation_messages_path(@conversation)
    end
  end
  
  private
  
  def message_params
    params.require(:message).permit(:body, :user_id)
  end
end

对话索引页面

<h3>Mailbox</h3>
   <% @conversations.each do |conversation| %>
   <% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %>
    <% if conversation.sender_id == current_user.id %>
      <% recipient = User.find(conversation.recipient_id) %>
    <% else %>
      <% recipient = User.find(conversation.sender_id) %>
    <% end %>
    <%= link_to recipient.firstname, conversation_messages_path(conversation)%>
   <% end %>
  <% end %>

<h3>All Users</h3>
 <% @users.each do |user| %>
  <% if user.id != current_user.id %>
   <%= user.firstname %> <%= link_to "Message me!", conversations_path(sender_id: current_user.id, recipient_id: user.id), method: "post"%>
  <% end %>
 <% end %>

消息索引页面

<% @messages.each do |message| %>
 <% if message.body %>
  <% user = User.find(message.user_id) %>
   <p><%= user.firstname %> and <%= message.message_time %></p>
   <p><%= message.body %></p>
 <% end %>
<% end %>

<%= form_for [@conversation, @message] do |f| %>
 <%= f.text_area :body %>
 <%= f.text_field :user_id, value: current_user.id %>
 <%= f.submit "Add Reply" %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您没有在 MessagesController#index 操作中定义 @message 变量。请这样定义

     def index
       @messages = @conversation.messages
       @message = @conversation.messages.new
     end
    

    【讨论】:

    • 非常感谢君安,我不小心把这个漏掉了..!!现在一切正常,非常感谢!
    • 欢迎南涅夫!
    猜你喜欢
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多