2014-02-12 45 views
2

嗯,我在Ruby on Rails的刪除方法有這樣的問題,我想,我試過了我讀的所有內容,但它不起作用,也許你可以幫助修復差距。 ?Rails刪除方法它不起作用

當我點擊鏈接重定向到患者/ 1確認=爲+你+確定%3F &方法=從Chrome的控制檯未捕獲的SyntaxError刪除

而且收到的消息:意外的記號=: ?3000 /資產/的application.js體= 1:13,其屬於

= require jquery

我的代碼是下面的:

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

<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

<!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' 

命令

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)

感謝您的幫助!

+0

「它不工作」用什麼樣的路徑? –

+0

我點擊鏈接它什麼都不做 – rfcabal

+0

也許這可以幫到你。 http://stackoverflow.com/questions/963420/undo-scaffolding-in-rails/963485#963485 –

回答

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

更新

的application.js應該

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

= require jquery 
= require jquery_ujs 
= require turbolinks 
= require_tree . 
+0

同樣我點擊鏈接,它在患者詳細信息「顯示」中重定向到我,但它不會從數據庫中刪除患者 – rfcabal

+0

您與jquery_ujs圖書館有鏈接嗎? – Jacek

+0

我從aplicattion.js中刪除//,那是什麼意思? – rfcabal

2

也許你有一個驗證錯誤。更新delete方法以在銷燬後使用bang,以顯示輸出。這應該至少可以幫助你調試你的現狀。否則提供更多的細節,我可以更新答案。

 

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

謝謝,問題解決了! – rfcabal

+0

良好的工作總是使用爆炸來拋出異常。 – wallerjake

0

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

你缺少的部分path

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

+1

我收到一個錯誤顯示C:/CursoTuby/proyecto01/simpleform/app/views/patients/show.html.erb其中第7行提出: 未定義的局部變量或方法'destroy_patient_path'爲#<#:0x42729c0> – rfcabal

0

您需要添加你要刪除的對象:

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

當我添加@patient時,它會在患者詳細信息「show」中重定向到我,並且它不會從數據庫中刪除患者。 – rfcabal

+0

在瀏覽器中分享鏈接的HTML。你使用的是什麼版本的Rails? – cortex

+0

我正在使用Rails 4.0.2 – rfcabal

0

你需要在你看來...有不同的方法來傳遞變量

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

然後在你的路線。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 

問題是你還沒有定義的路線,你是不是提供調用