MessagesController#destroy中的ActiveRecord :: RecordNotFound

时间:2018-04-03 04:50:02

标签: ruby-on-rails

我是RoR的初学者,我正在尝试向我的应用程序中的表添加一个复选框功能(选择多个数据记录复选框并将其删除)。当我选中一个复选框并单击删除按钮时,我收到了一个错误(如标题中所述)。

这是我的控制器文件:

def destroy
  @message = Message.find(params[:id])
  @message.destroy
  respond_to do |format|
    format.html { redirect_to messages_url, notice: 'Message was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_message
  @message = Message.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def message_params
  params.require(:message).permit(:content)
end

这是我的index.html文件:

<h1>Messages</h1>
<%= form_tag messages_path, method: :delete do %>
<table>
  <thead>
    <tr>
      <th>Content</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>

    <% @messages.each do |message| %>
      <tr>
        <td><%= check_box_tag "message_ids[]", message.id%></td>
        <td><%= message.content %></td>
        <td><%= link_to 'Show', message %></td>
        <td><%= link_to 'Edit', edit_message_path(message) %></td>
        <!-- <td><%= link_to 'Destroy', message, method: :delete, data: { confirm: 'Are you sure?' } %></td> -->
      </tr>
    <% end %> 
  </tbody>
</table>
<br>
<%= button_to "Delete", message_path(@messages),method: :delete%>
</br>
<% end %>
<br>
<%= link_to  'New Message', new_message_path %> 
</br>

这是我的routes.rb文件:

Rails.application.routes.draw do
  resources :messages do
    collection {delete :destroy}    
  end
  root to: "messages#index"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

错误显示:

  

无法找到带有'id'=

的消息      

摘录来源(第69行):67 68 69 70 71 72

# Use callbacks to share common setup or constraints between actions.
def set_message
  @message = Message.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
     

请帮我辨别我的错误......

3 个答案:

答案 0 :(得分:1)

问题出在“删除”按钮上,它应该是这样的。

<h1>Messages</h1>
<%= form_tag messages_path, method: :delete do %>
<table>
  <thead>
    <tr>
      <th>Content</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>

    <% @messages.each do |message| %>
      <tr>
        <td><%= check_box_tag "message_ids[]", message.id%></td>
        <td><%= message.content %></td>
        <td><%= link_to 'Show', message %></td>
        <td><%= link_to 'Edit', edit_message_path(message) %></td>
        <!-- <td><%= link_to 'Destroy', message, method: :delete, data: { confirm: 'Are you sure?' } %></td> -->
      </tr>
    <% end %> 
  </tbody>
</table>
<br>
<%#= button_to "Delete", message_path(@messages),method: :delete%>
<%= submit_tag :Delete,class: 'btn' %>

</br>
<% end %>
<br>
<%= link_to  'New Message', new_message_path %> 
</br>

提交表单后,您将在params[:message_ids]

的控制器中获得一组消息ID

所以在破坏行动: -

def destroy
      #@message = Message.find(params[:id])
      @messages = Message.where(id: params[:message_ids])
      #@messages.destroy
      @messages.destroy_all
      respond_to do |format|
      format.html { redirect_to messages_url, notice: 'Message was successfully destroyed.' }
      format.json { head :no_content }
    end
end

注意: - 如果您在使用destroy方法之前使用destroy方法,则不要使用before_action回调来进行destroy方法;

before_action :set_message, only: [:show, :edit, :update]

答案 1 :(得分:0)

看起来您正在尝试选择具有ID数组的模型。你可能想要做一些像Message.where(:id =&gt; params [:ids])。destroy_all。 params [:ids] = [1,2,3等]。

答案 2 :(得分:0)

您可以将其分为两种方法。

在控制器中创建destroy_multiple

import csv
with open('sensorFeature.csv', 'r') as sensorRawFile:
    rawData = csv.reader(sensorRawFile)

    with open('new.csv', 'w') as sensorFeatureFile:
        csv_writer = csv.writer(sensorFeatureFile)

        for row in rawData:
            for i in range(len(row)):
                row[i] = int(row[i])
            csv_writer.writerow(row)

# checking if I change string to int successfully
with open('new.csv', 'r') as test:
    reader = csv.reader(test)
    for line in reader:
        for x in line:
            print(type(x))
#  the result still tell me they are all string instead of int

修改了你的routes.rb

def destroy_multiple
  @messages = Message.where(id: params[:message_ids])
  @messages.destroy_all
  respond_to do |format|
    format.html { redirect_to messages_url, notice: 'Message was successfully destroyed.' }
    format.json { head :no_content }
  end
end

你的html文件。

Rails.application.routes.draw do
  resources :messages do
    collection { delete :destroy_multiple }    
  end
  root to: "messages#index"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

4。您无需更改<%= form_tag destroy_multiple_messages_path, method: :delete do %> <table> <thead> <tr> <th>Content</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @messages.each do |message| %> <tr> <td><%= check_box_tag "message_ids[]", message.id%></td> <td><%= message.content %></td> <td><%= link_to 'Show', message %></td> <td><%= link_to 'Edit', edit_message_path(message) %></td> <td><%= link_to 'Destroy', message, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= button_to "Delete", message_path(@messages), method: :delete%> </br> <% end %> <br> <%= link_to 'New Message', new_message_path %> </br> 方法

destroy

结论,您有两个删除功能。我希望这能帮到你。