Ruby on rails RubyonRails不使用脚手架进行删除
我是RoR新手,在RubyonRails上遇到了一个问题,因为当我单击“销毁”按钮时,它不会删除记录。 我创建了一个新项目,并使用命令scaffold来创建它 我的观点:Ruby on rails RubyonRails不使用脚手架进行删除,ruby-on-rails,Ruby On Rails,我是RoR新手,在RubyonRails上遇到了一个问题,因为当我单击“销毁”按钮时,它不会删除记录。 我创建了一个新项目,并使用命令scaffold来创建它 我的观点: <h1>CARTELERA</h1> <table> <thead> <tr> <th>Titulo</th> <th>Genero</th> <th>Dir
<h1>CARTELERA</h1>
<table>
<thead>
<tr>
<th>Titulo</th>
<th>Genero</th>
<th>Director</th>
<th>Duracion</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @peliculas.each do |pelicula| %>
<tr>
<td><%= pelicula.titulo %></td>
<td><%= pelicula.genero %></td>
<td><%= pelicula.director %></td>
<td><%= pelicula.duracion %></td>
<td><%= link_to 'Mostrar', pelicula %></td>
<td><%= link_to 'Editar', edit_pelicula_path(pelicula) %></td>
<td><%= link_to 'Eliminar', pelicula, method: :delete, data: { confirm: 'Estás seguro?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'Nueva Película', new_pelicula_path %>
def destroy
@pelicula.destroy
respond_to do |format|
format.html { redirect_to peliculas_url, notice: 'Pelicula was successfully destroyed.' }
format.json { head :no_content }
end
end
输出耙路由
C:\Cine>rake routes
Prefix Verb URI Pattern Controller#Action
peliculas GET /peliculas(.:format) peliculas#index
POST /peliculas(.:format) peliculas#create
new_pelicula GET /peliculas/new(.:format) peliculas#new
edit_pelicula GET /peliculas/:id/edit(.:format) peliculas#edit
pelicula GET /peliculas/:id(.:format) peliculas#show
PATCH /peliculas/:id(.:format) peliculas#update
PUT /peliculas/:id(.:format) peliculas#update
DELETE /peliculas/:id(.:format) peliculas#destroy
root GET / peliculas#index
我的控制器已完成
class PeliculasController < ApplicationController
before_action :set_pelicula, only: [:show, :edit, :update, :destroy]
# GET /peliculas
# GET /peliculas.json
def index
@peliculas = Pelicula.all
end
# GET /peliculas/1
# GET /peliculas/1.json
def show
end
# GET /peliculas/new
def new
@pelicula = Pelicula.new
end
# GET /peliculas/1/edit
def edit
end
# POST /peliculas
# POST /peliculas.json
def create
@pelicula = Pelicula.new(pelicula_params)
respond_to do |format|
if @pelicula.save
format.html { redirect_to @pelicula, notice: 'Pelicula was successfully created.' }
format.json { render :show, status: :created, location: @pelicula }
else
format.html { render :new }
format.json { render json: @pelicula.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /peliculas/1
# PATCH/PUT /peliculas/1.json
def update
respond_to do |format|
if @pelicula.update(pelicula_params)
format.html { redirect_to @pelicula, notice: 'Pelicula was successfully updated.' }
format.json { render :show, status: :ok, location: @pelicula }
else
format.html { render :edit }
format.json { render json: @pelicula.errors, status: :unprocessable_entity }
end
end
end
# DELETE /peliculas/1
# DELETE /peliculas/1.json
def destroy
@pelicula.destroy
respond_to do |format|
format.html { redirect_to peliculas_url, notice: 'Pelicula was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pelicula
@pelicula = Pelicula.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def pelicula_params
params.require(:pelicula).permit(:titulo, :genero, :director, :duracion)
end
end
class PeliculasController
我想这是因为你的语法错了。尝试添加此:方法=>:删除,现在您有
。下面是一种调试是否发送请求的方法
在本地运行服务器时,请检查终端窗口中的日志
当服务器运行时,在终端内按几次enter键,将在最后一个操作和应用程序将执行的新操作之间提供一些负面空间
在终端中添加一些负空间后,返回应用程序并按项目上的delete键
回到你的终端,如果你看到的话,找到空间的空隙
已开始删除“/peliculas/{Some Number}”
通过PeliculasController#销毁为HTML进行处理
这意味着它正在工作
我很确定这可能是因为您在“Eliminar”之后的语法错误,请将其更改为,:method=>:delete
而不是method::delete
编辑问题…单击“delete”按钮后会发生什么?单击“delete”按钮时,请尝试此处的答案,是否刷新页面?如果是这样的话,那么您的js
part似乎有问题,Rails中的GET请求是如何处理的。在JavaScript中是否正确地需要它?