我想通过索引页上的按钮从数据库检索特定记录,并将该对象存储在临时数组中,然后在特定视图中显示对象的属性。但是find方法的记录没有存储在一个变量中,我不知道为什么......我得到nil的错误:NilClass。(在我的项目中,我希望能够为一个有序病人列出一个天,并在视图中显示它们)从数据库到数组的记录
这是我的代码:
patient_index.html.erb(从支架)<tbody>
<% @patients.each do |patient| %>
<tr>
<td><%= patient.name %></td>
<td><%= patient.last_name %></td>
<td><%= patient.id %></td>
<td><%= link_to 'Show', patient %></td>
<td><%= link_to 'Edit', edit_patient_path(patient) %></td>
<td><%= link_to 'Destroy', patient, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td> <%= button_to 'Order', order_path(id: patient)%></td>
<th><%= @temp[0].name %></th> #testing if it shows the value here
</tr>
<% end %>
的routes.rb
post 'order', to: 'patients#order'
patients_controller.rb
def index
@patients = Patient.all
end
def show
end
def edit ... #generated by scaffold
def destroy ...
def update ...
def order
@temp = []
@the_patient = Patient.find(params[:id])
@temp << @the_patient
redirect_to patients_url
end
错误图像链接http://imgur.com/nJdyR2E求助
感谢,并建议:)
你在哪里定义'@ patients'?也请发布完整的错误信息与相关的回溯。 – shivam 2015-03-31 18:11:34
抱怨说'@ patients'不再存在,因为你没有在'#order'中定义它。 – engineersmnky 2015-03-31 18:46:32
当你重定向时,变量'@ temp'丢失,如果你愿意的话,将这段代码移动到索引动作 – 2015-04-01 18:10:54