在构建会话+消息模型时,消息#index中的ArgumentError

时间:2016-10-24 23:59:14

标签: ruby-on-rails

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

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

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

  消息#index

中的

ArgumentError      

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

     

表单中的第一个参数不能包含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,但在Message视图文件中出错了。

有没有人知道我错过了什么?我对Rails很陌生,我现在已经盲目地盯着它看了2个晚上。非常感谢您的帮助!

的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 %>

1 个答案:

答案 0 :(得分:0)

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

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