我是初学者。在我的ror应用程序中,我使用了一个字段的scaffold函数,我只需添加一个复选框功能。现在我尝试选择多个复选框(记录)并从表中删除该记录。 当我单击删除按钮时,我收到了路由错误。请帮我纠正我的错误。
这是我的Controller.rb文件:
class MessagesController < ApplicationController
before_action :set_message, only: [:show, :edit, :update, :destroy]
# GET /messages
# GET /messages.json
def index
@messages = Message.all
end
# GET /messages/1
# GET /messages/1.json
def show
# @message = Message.find(params[:id])
end
# GET /messages/new
def new
@message = Message.new
end
# GET /messages/1/edit
def edit
# @message = Message.find(params[:id])
end
# POST /messages
# POST /messages.json
def create
@message = Message.new(message_params)
respond_to do |format|
if @message.save
format.html { redirect_to @message, notice: 'Message was successfully created.' }
format.json { render :show, status: :created, location: @message }
else
format.html { render :new }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /messages/1
# PATCH/PUT /messages/1.json
def update
respond_to do |format|
if @message.update(message_params)
format.html { redirect_to @message, notice: 'Message was successfully updated.' }
format.json { render :show, status: :ok, location: @message }
else
format.html { render :edit }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
end
# DELETE /messages/1
# DELETE /messages/1.json
def destroy
@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
end
这是index.html.erb文件:
<p id="notice"><%= notice %></p>
<h1>Messages</h1>
<table>
<thead>
<tr>
<th>Content</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @messages.each do |message| %>
<tr>
<%= form_tag messages_path, :method => 'delete' do %>
<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>
<% end %>
<%= button_to 'Delete', messages_path %>
</table>
<br>
<%= link_to 'New Message', new_message_path %>
</br>
My Routes.rb文件:
Rails.application.routes.draw do
resources :messages do
collection {post :destroy}
end
root to: "messages#index"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
错误显示:
路由错误没有路由匹配[DELETE]“/ messages”
答案 0 :(得分:0)
:)
你可以像这样编辑routes.rb:
resources :message
而不是你的:
resources :messages do
collection {post :destroy}
end
用于销毁的HTTP方法是“DELETE”,而不是“POST”。
如果你运行'rails routes',你必须看到如下行:
DELETE /messages/:id(.:format)
谢谢:)