Ruby on rails 添加发送消息链接时显示ActiveRecord\u关联的rails出错
我有一个rails应用程序,用户可以在其中添加帖子,其他用户可以对其进行评论。我试图添加一个与评论用户的直接消息链接,我在stackoverflow的建议下尝试了类似的方法 \u form.html.erbRuby on rails 添加发送消息链接时显示ActiveRecord\u关联的rails出错,ruby-on-rails,messaging,mailboxer,Ruby On Rails,Messaging,Mailboxer,我有一个rails应用程序,用户可以在其中添加帖子,其他用户可以对其进行评论。我试图添加一个与评论用户的直接消息链接,我在stackoverflow的建议下尝试了类似的方法 \u form.html.erb <%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %> <div class="form-group"> <%= f.label :recipients %
<%= 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 %>
<%= 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
show.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 %>
<%= 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
@post.comments.user.id),类:“发送消息图标”%>
**会话_controller.rb**
def新
@user=user.find_by(id:params[:recipient_id])
结束
def创建
recipients=User.find_by(id:params[:recipient_id])
会话=当前用户。发送消息(收件人、会话参数[:正文]、会话参数[:主题])。会话
flash[:success]=“您的邮件已成功发送!”
回应待办事项|格式|
format.html{重定向到对话路径(对话)}
format.js
结束
结束
但是我得到了以下错误
undefined method `user' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x93b1018>
的未定义方法“user”#
我正在使用mailboxer gem来实现消息传递功能。假设您有一个
注释
表,其中有一列用户id
<% @post.comments.each do |comment| %>
<%= link_to 'Send Message', new_conversation_path(recipient_id: comment.user_id) %>