Ruby-on-rails Rails 删除方法不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21739122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:21:37  来源:igfitidea点击:

Rails delete method it doesn't work

ruby-on-railsrubydelete-method

提问by rfcabal

Well I'm having such a problem with delete method in Ruby on Rails, i think, i tried everything i read but it doesn't work maybe you gusy can help fix the gap.

好吧,我在 Ruby on Rails 中的 delete 方法遇到了这样的问题,我想,我尝试了我阅读的所有内容,但它不起作用也许您可以帮助解决差距。

When I click the link It redirect to patients/1?confirm=Are+you+sure%3F&method=delete

当我点击链接时它重定向到患者/1?confirm=Are+you+sure%3F&method=delete

But also a received a message from my chrome console Uncaught SyntaxError: Unexpected token = :3000/assets/application.js?body=1:13 which belong to

但也收到了来自我的 chrome 控制台 Uncaught SyntaxError: Unexpected token = :3000/assets/application.js?body=1:13 的消息,属于

= require jquery

= 需要 jquery

My code is following:

我的代码如下:

patients.controller

患者控制员

def show
    @patient = Patient.find(params[:id])
end

def new
    @patient = Patient.new
    @patients = Patient.find(:all)
end


def create
    @patient = Patient.new(params[:patient])
    if  @patient.save
        redirect_to new_patient_path
    end
end

def edit
    @patient = Patient.find(params[:id])
end

def update
    @patient = Patient.find(params[:id])
    if @patient.update_attributes(params[:patient])
     redirect_to :action => 'show', :id => @patient
    else
     @patient = patient.find(:all)
     render :action => 'edit'
    end
end

def destroy
    @patient = Patient.find(params[:id])
    @patient.destroy
    redirect_to '/patients/new', :notice => "Your patient has been deleted"
end

show.html.erb

显示.html.erb

<h2>Patient Information</h2>
<%= @patient.firstname %><br /> 
<%= @patient.lastname %><br />
<%= @patient.phone %><br /> 
<p> <%= link_to "Edit", edit_patient_path %> |  <%= link_to "Delete", :confirm => "Are you sure?", :method => :delete %> </p>

application js

应用程序js

= require jquery
= require jquery_ujs
= require turbolinks
= require_tree .

application.html.erb

应用程序.html.erb

<!DOCTYPE html>
<html>
<head>
<title>Registration Patient System</title>
<%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

routes

路线

  resources :patients
  match 'patients/:id' => 'patients#show', via: [:get, :post]

  resources :users
  match '/register', to: 'users#new', via: [:get, :post]

  resources :pages

  resources :sessions, :only => [:new, :create, :destroy]
  match '/login', to: 'sessions#new', via: [:get, :post]
  match '/logout', to: 'sessions#destroy', via: [:get, :post]

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'sessions#new'

Command Line

命令行

Started GET "/patients/1?confirm=Are+you+sure%3F&method=delete" for 127.0.0.1 at 2014-02-12 18:14:18 -0300 Processing by PatientsController#show as HTML Parameters: {"confirm"=>"Are you sure?", "method"=>"delete", "id"=>"1"} [1m[36mPatient Load (1.0ms)[0m [1mSELECT "patients".* FROM "patients" WHERE "patients"."id" = ? LIMIT 1[0m [["id", "1"]] Rendered patients/show.html.erb within layouts/application (1.0ms) Completed 200 OK in 13ms (Views: 9.0ms | ActiveRecord: 1.0ms)

开始 GET "/patients/1?confirm=Are+you+sure%3F&method=delete" for 127.0.0.1 at 2014-02-12 18:14:18 -0300 处理由PatientsController#show as HTML Parameters: {"confirm" =>“你确定吗?”,“方法”=>“删除”,“id”=>“1”} [1m[36mPatient Load (1.0ms)[0m [1mSELECT “patients”.* FROM “patients” WHERE “患者”。“id”=?LIMIT 1[0m [["id", "1"]] 在布局/应用程序中渲染病人/show.html.erb (1.0ms) 在 13ms 内完成 200 OK (Views: 9.0ms | ActiveRecord: 1.0ms)

Thanks for your help!

谢谢你的帮助!

采纳答案by Zoki

<%= link_to "Delete", patient_path(@patient), :confirm => "Are you sure?", :method => :delete %>

update

更新

application.js should be

application.js 应该是

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

NOT

不是

= require jquery
= require jquery_ujs
= require turbolinks
= require_tree .

回答by wallerjake

Maybe you are having a validation error. Update the delete method to use the bang after the destroy so it shows output. That should at least help you debug your current situation. Otherwise provide more details and I can update answer.

也许您遇到了验证错误。更新 delete 方法以在销毁后使用 bang 以显示输出。这至少应该可以帮助您调试当前的情况。否则提供更多详细信息,我可以更新答案。


def destroy
    @patient = Patient.find(params[:id])
    @patient.destroy!
    redirect_to '/patients/new', :notice => "Your patient has been deleted"
end

回答by CaptChrisD

You need in your view... there are different ways to pass the variable

你需要在你看来......有不同的方法来传递变量

<%= link_to "Delete", destroy_patient_path, :confirm => "Are you sure?", :method => :delete %>

Then in your routes.rb

然后在你的 routes.rb

#more member routes probably need definition but not showing them here
resources :patients do
    member do
        get  :show
        post :show
        delete :destroy
    end
end

The problem is you have not defined the route and you are not providing the path to call

问题是你没有定义路由,也没有提供调用路径

回答by xlembouras

in

<%= link_to "Delete", :confirm => "Are you sure?", :method => :delete %>

<%= link_to "Delete", :confirm => "Are you sure?", :method => :delete %>

you are missing the pathpart

你错过了这个path部分

<%= link_to "Delete", destroy_patient_path, :confirm => "Are you sure?", :method => :delete %>

<%= link_to "Delete", destroy_patient_path, :confirm => "Are you sure?", :method => :delete %>

回答by cortex

You need to add the object you want to delete:

您需要添加要删除的对象:

<%= link_to "Delete", @patient, :confirm => "Are you sure?", :method => :delete %>