2

まあ、私はRuby on Railsのdeleteメソッドでこのような問題を抱えています.読んだことはすべて試しましたが、うまくいかないかもしれません.

リンクをクリックすると患者にリダイレクトされます/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

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'

コマンドライン

2014-02-12 18:14:18 -0300 で 127.0.0.1 の GET "/patients/1?confirm=Are+you+sure%3F&method=delete" を開始しました。 =>"よろしいですか?", "メソッド"=>"削除", "id"=>"1"} [1m[36m患者負荷 (1.0ms)[0m [1mSELECT "患者".* FROM "患者" WHERE "患者"."id" = ? LIMIT 1[0m [["id", "1"]] レイアウト/アプリケーション内の患者/show.html.erb をレンダリング (1.0ms) 13ms で 200 OK を完了 (ビュー: 9.0ms | ActiveRecord: 1.0ms)

ご協力いただきありがとうございます!

4

5 に答える 5

2

検証エラーが発生している可能性があります。出力が表示されるように、destroy の後に bang を使用するように delete メソッドを更新します。これは、少なくとも現在の状況をデバッグするのに役立つはずです。それ以外の場合は、詳細を提供してください。回答を更新できます。


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

于 2014-02-12T20:49:27.560 に答える
0

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

pathあなたはその部分が欠けています

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

于 2014-02-12T20:49:37.543 に答える
0

ビューで必要です...変数を渡すにはさまざまな方法があります

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

次に、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

問題は、ルートを定義しておらず、呼び出すパスを提供していないことです

于 2014-02-13T00:02:55.410 に答える