rails添加发送消息链接时显示ActiveRecord_Associations错误

时间:2016-02-07 16:30:07

标签: ruby-on-rails messaging mailboxer

我有一个rails应用程序,用户可以添加帖子,其他用户可以评论他们我试图添加直接消息链接与评论的用户,我尝试了类似这样的建议在stackoverflow

_form.html.erb

<%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %>
<div class="form-group">
  <%= f.label :recipients %>
  <%= hidden_field_tag(:recipient_id, "#{@user.id}") %></div>
<div class="form-group">
  <%= f.label :subject %>
  <%= f.text_field :subject, placeholder: "Subject", class: "form-control" %>
</div>
<div class="form-group">
  <%= f.label :message %>
  <%= f.text_area :body, class: 'form-control',placeholder: "Type your message here", rows: 4  %>
</div>

<%= f.submit "Send Message", class: "btn btn-success" %>

<% end %>

show.html.erb

<%= link_to 'Send Message', new_conversation_path(:recipient_id => @post.comments.user.id), class: 'send-message-icon' %>

**conversation_controller.rb**
def new
    @user = User.find_by(id: params[:recipient_id])
  end

   def create
    recipients = User.find_by(id: params[:recipient_id])
    conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
    flash[:success] = "Your message was successfully sent!"
    respond_to do |format|
      format.html {redirect_to conversation_path(conversation)}
      format.js
    end
  end

但是我收到了以下错误

undefined method `user' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x93b1018>

我正在使用mailboxer gem来实现消息传递功能。

1 个答案:

答案 0 :(得分:0)

假设您有一个comments表,其中包含user_id

<% @post.comments.each do |comment| %>
  <%= link_to 'Send Message', new_conversation_path(recipient_id: comment.user_id) %>