0

我已经得到它,以便我有一个临床医生正在与之交谈的独特患者的列表。我可以调出有关患者的详细信息,但无法调出与相关评论有关的任何内容。

app\views\comments\index.html.erb

    <% @comments.map(&:patient).uniq.each_with_index do |comment, index| %>
      <div class="profile-post color-one">
          <span class="profile-post-numb"><%= index+1 %></span>
          <div class="profile-post-in">
              <h3 class="heading-xs"><a><%= link_to "#{comment.first_name} #{comment.last_name}", comment_path(comment) %></a></h3>
              <p><%= comment.diagnosis %><i class="pull-right"><%= link_to "Edit", edit_comment_path(comment) %> <%= link_to "Delete", comment_path(comment), method: :delete, data: {confirm: 'Are you sure you want to delete'} %></i></p>
          </div>
      </div>
    <% end %>

link_to 也被破坏了,因为它们应该指向评论页面,但传递的是 patient.id 而不是 comment.id。

comments_conrtoller.rb

def index
 @comments = current_clinician.comments
end

评论.rb

class Comment < ActiveRecord::Base
 belongs_to :clinician
 belongs_to :patient
end
4

1 回答 1

0

@comments.map(&:patient)给出属于 class 的对象,Patient这就是为什么链接通过patient_id而不是comment_id.

你应该使用group而不是在map这里。

类似的东西@comments.group(:patient)..; 必须更改循环语法以使用此范围的输出。

有关如何工作的更多详细信息,请参阅http://apidock.com/rails/v4.0.2/ActiveRecord/QueryMethods/groupgroup

于 2015-03-16T04:46:07.287 回答