本文介绍了Rails 删除方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

= 需要 jquery

我的代码如下:

患者.控制器

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

show.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>

应用js

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

application.html.erb

application.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>

路线

  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'

命令行

开始 GET "/patients/1?confirm=Are+you+sure%3F&method=delete"127.0.0.1 at 2014-02-12 18:14:18 -0300 由PatientsController#show 处理为HTML 参数:{"confirm"=>"Are you确定吗?", "method"=>"delete", "id"=>"1"} [1m[36mPatient Load (1.0ms)[0m[1mSELECT "患者".* FROM "患者" WHERE "患者"."id" = ?限制1[0m [["id", "1"]] 渲染的患者/show.html.erb 内布局/应用程序(1.0 毫秒)在 13 毫秒内完成 200 个 OK(视图:9.0 毫秒 |活动记录:1.0 毫秒)

感谢您的帮助!

推荐答案

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

更新

application.js 应该是

application.js should be

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

不是

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

这篇关于Rails 删除方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 15:31