好吧,我在 Ruby on Rails 中的删除方法遇到了这样的问题,我想,我尝试了我阅读的所有内容,但它不起作用也许你可以帮助解决这个差距。
当我单击链接时它重定向到患者/1?confirm=Are+you+sure%3F&method=delete
但也从我的 chrome 控制台收到一条消息 Uncaught SyntaxError: Unexpected token = :3000/assets/application.js?body=1:13 属于
= 需要 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
显示.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 .
应用程序.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'
命令行
在 2014-02-12 18:14:18 -0300 开始 GET "/patients/1?confirm=Are+you+sure%3F&method=delete" for 127.0.0.1 18:14:18 -0300 由 PatientController#show 作为 HTML 参数处理:{"confirm" =>“你确定吗?”、“方法”=>“删除”、“id”=>“1”} [1m[36mPatient Load (1.0ms)[0m [1mSELECT "patients".* FROM "patients" WHERE “病人”。“身份证”=?LIMIT 1[0m [["id", "1"]] 在布局/应用程序中渲染患者/show.html.erb (1.0ms) 在 13ms 内完成 200 OK (Views: 9.0ms | ActiveRecord: 1.0ms)
谢谢你的帮助!