简体   繁体   English

Rails DELETE http方法呈现页面

[英]Rails DELETE http method renders page

I am trying to add a delete functionality to my model. 我正在尝试向我的模型添加删除功能。 This is what I've come up with, but even though I don't need to render the page to delete something, Rails renders and couldn't find the file for "delete.html.erb" 这就是我想出来的,但即使我不需要渲染页面来删除某些内容,Rails也会渲染并找不到“delete.html.erb”的文件

I am using Ruby 2.0dev and Rails 4.0 我正在使用Ruby 2.0dev和Rails 4.0

My delete link: 我的删除链接:

<%= link_to "Delete", reservation_delete_path(item), :class => "btn btn-small btn-danger", method: :delete, data: {confirm: 'Are you sure?'} %></td>

My routes file: 我的路线档案:

match 'reservations/delete/:id' => 'reservations#delete', via: :delete, :as => 'reservation_delete'

My Controller: 我的控制器:

def delete
  @current = Reservations.find(params[:id])
  if current_user
    if @current.user_id == current_user.id
      @current.destroy!
      redirect_to reservations_path
    else
      redirect_to reservations_path
    end
  else
    redirect_to reservations_path
  end
end

There is no need to duplicate the redirect 3 times for each condition. 对于每个条件,无需复制重定向3次。 You can simplify your delete method: 您可以简化删除方法:

def delete
  @current = Reservations.find(params[:id])

  if current_user && @current.user_id == current_user.id
    @current.destroy!
  end

  redirect_to reservations_path
end

In your question, if current_user isn't available, you have no redirect, and so an implicit render is being run. 在您的问题中,如果current_user不可用,则您没有重定向,因此正在运行隐式渲染。

Your setup is not idiomatic, and there's code you didn't include, so anything could be going wrong. 你的设置不是惯用的,而且你没有包含的代码,所以任何事情都可能出错。 For example, that can't be your whole routes file; 例如,那不能是你的整个路线文件; there's nothing specifying an index/show/edit/whatever page where your delete button would be. 没有指定索引/显示/编辑/删除按钮所在的任何页面。 Another example: your action is named delete instead of destroy . 另一个例子:你的行动被命名为delete而不是destroy Anyway I can show you an example that works and is much more canonical: 无论如何,我可以向你展示一个有效的例子,并且更具规范性:

models/reservation.rb: 车型/ reservation.rb:

class Reservation < ActiveRecord::Base
end

controllers/reservations_controller.rb: 控制器/ reservations_controller.rb:

class ReservationsController < ApplicationController
  def index
    @reservations = Reservation.all
  end

  def destroy
    @reservation = Reservation.find(params[:id])
    @reservation.destroy

    redirect_to reservations_url
  end
end

views/reservations/index.html.erb: 意见/预订/ index.html.erb:

<% @reservations.each do |reservation| %>
  <%= link_to 'Destroy', reservation, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

(this will literally only show links for deleting corresponding reservations... you'll have to stick <%= reservation.name %> or whatever in there if you want to see more info) (这将只显示删除相应预订的链接...如果您想查看更多信息,您必须坚持<%= reservation.name %>或其他任何内容)

config/routes.rb: 配置/ routes.rb文件:

Howdy::Application.routes.draw do
  resources :reservations, only: [:index, :destroy]
  root 'reservations#index'
end

(my app name is howdy) (我的应用名称是你的)

You have some user auth going on, so add that accordingly. 您正在进行一些用户身份验证,因此请相应添加。 If you're inheriting from a controller that does special user-auth stuff before even hitting the action, that might be why it's trying to render delete.html.erb 如果您在执行操作之前从一个执行特殊用户身份验证的控制器继承,这可能就是为什么它试图渲染delete.html.erb

看起来你错过了那些重定向的返回,这实际上导致Rails执行重定向尝试渲染视图。

return redirect_to reservations_path

Two things: 两件事情:

The delete (destroy) action is part of resources when you specify it in the routes file. 在routes文件中指定delete(destroy)操作是资源的一部分。 To do this the "rails" way, you might consider having your routes file look more like: 要以“rails”的方式执行此操作,您可以考虑让路由文件看起来更像:

resources: :reservations, only: [:delete]

... then having the delete link be more like: ...然后删除链接更像是:

<%= link_to 'Delete', delete_reservation_path(item), :class => 'btn btn-small btn-danger', method: :delete, data: {confirm: 'Are you sure?'} %>

... and then in your controller you can: ...然后在您的控制器中,您可以:

def destroy
  @current = Reservations.find(params[:id])
  if current_user
    if @current.user_id == current_user.id
      @current.destroy!
      redirect_to reservations_path
    else
      redirect_to reservations_path
    end
  else
    redirect_to reservations_path
  end
end

... or you could actually create an rjs template for the delete action to do some fancy javascript work, or you could simply render the view for the index action (faster load the redirecting). ...或者您实际上可以为删除操作创建一个rjs模板来执行一些奇特的javascript工作,或者您可以简单地渲染索引操作的视图(更快地加载重定向)。

My recommendation when you start putting up && gates is to check to see if there is an existing solution. 当你开始提出&& gate时我的建议是检查是否有现有的解决方案。 In this case you're probably looking for the functionality that is available in the CanCan gem. 在这种情况下,您可能正在寻找CanCan gem中可用的功能。

CanCan 可以可以

basically you load_and_authorize your users before the controller action and check them through an Ability model. 基本上你在控制器操作之前对用户进行load_and_authorize ,并通过Ability模型检查它们。 You also get view helpers like 您也可以获得视图助手

if can? :destroy, reservation 
  ... do awesome stuff here ...

This will be a far better solution over the long run. 从长远来看,这将是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM