我是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>
这是我的路由。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.
请帮助我确定我的错误..
问题是您的删除按钮,应该是这样的。
<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
注意: - 如果您在采取行动以进行销毁方法,则不要将_ATCONT回调用于销毁方法;
before_action :set_message, only: [:show, :edit, :update]
看起来您正在尝试选择具有ID数组的模型。您可能想执行诸如message.where(:id => params [:ids](的事情。其中params [:ids] = [1,2,3等]。
您可以将其分为两种方法。
在控制器中创建Destry_multiple
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
修订了您的路由。RB
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
您的HTML文件。
<%= 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>
4。您不需要更改destroy
方法
def destroy
@message = Message.find(params[:id])
@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
结论,您有两个删除功能。我希望这对您有帮助。