2

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.

When I click the link It redirect to patients/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

= require 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

<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

= 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>

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)

Thanks for your help!

rfcabal
  • 121
  • 1
  • 4
  • 17
  • "it doesn't work" in what way? – David Aldridge Feb 12 '14 at 20:46
  • I'm clicking the link it doesn't do anything – rfcabal Feb 12 '14 at 20:48
  • Maybe this can help. http://stackoverflow.com/questions/963420/undo-scaffolding-in-rails/963485#963485 – Ariel Spear Feb 12 '14 at 20:51
  • @rfcabal It's almost impossible for it not to do *anything*. Trouble-shoot. Is the browser making a request? What's in the request/on the wire? What comes in to the controller? Etc. – Dave Newton Feb 12 '14 at 20:54
  • Oh yes, I click the link and i got the url http://localhost:3000/patients/39?confirm=Are+you+sure%3F&method=delete but it doesn't delete my patient – rfcabal Feb 12 '14 at 21:00
  • What i got in my command line is: 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) – rfcabal Feb 12 '14 at 21:15

5 Answers5

3

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.


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

Jacob Waller
  • 4,119
  • 3
  • 26
  • 32
3
<%= link_to "Delete", patient_path(@patient), :confirm => "Are you sure?", :method => :delete %>

update

application.js should be

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

NOT

= require jquery
= require jquery_ujs
= require turbolinks
= require_tree .
Zoki
  • 121
  • 4
1

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

#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

CaptChrisD
  • 195
  • 5
0

in

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

you are missing the path part

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

xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • 1
    I got an error Showing C:/CursoTuby/proyecto01/simpleform/app/views/patients/show.html.erb where line #7 raised: undefined local variable or method `destroy_patient_path' for #<#:0x42729c0> – rfcabal Feb 12 '14 at 20:54
0

You need to add the object you want to delete:

<%= link_to "Delete", @patient, :confirm => "Are you sure?", :method => :delete %>
cortex
  • 5,036
  • 3
  • 31
  • 41