Rails删除方法它不起作用
好吧,我在Ruby on Rails中遇到删除方法存在这样的问题,我想,我尝试了所有我读过的但是它不起作用也许你gusy可以帮助修复差距。
当我点击链接它重定向到患者/ 1?确认=是+你+确定%3F&方法=删除
但是从我的chrome控制台收到一条消息Uncaught SyntaxError:Unexpected token =:3000 / assets / application.js?body = 1:13属于
=需要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
Patient Information
| "Are you sure?", :method => :delete %>
申请js
= require jquery = require jquery_ujs = require turbolinks = require_tree .
application.html.erb
Registration Patient System true %>
路线
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?确认=你+你+确定%3F&方法=删除”为127.0.0.1由PatientsController处理#show as HTML参数:{“confirm” >>“你确定吗?”,“方法”=>“删除”,“id”=>“1”} [1m [36mPatient Load(1.0ms)[0m [1mSELECT“患者”。*来自“患者”WHERE “病人”。“id”=? LIMIT 1 [0m [[“id”,“1”]]在布局/应用程序内渲染patient / show.html.erb(1.0ms)在13ms完成200 OK(浏览次数:9.0ms | ActiveRecord:1.0ms)
谢谢你的帮助!
<%= 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 .
也许你有一个validation错误。 更新delete方法以在destroy之后使用bang,因此它显示输出。 这应该至少可以帮助您调试当前的情况。 否则提供更多细节,我可以更新答案。
def destroy @patient = Patient.find(params[:id]) @patient.destroy! redirect_to '/patients/new', :notice => "Your patient has been deleted" end
def destroy @patient = Patient.find(params[:id]) @patient.destroy! redirect_to '/patients/new', :notice => "Your patient has been deleted" end
在
<%= link_to "Delete", :confirm => "Are you sure?", :method => :delete %>
你错过了path
部分
<%= link_to "Delete", destroy_patient_path, :confirm => "Are you sure?", :method => :delete %>
您需要添加要删除的对象:
<%= link_to "Delete", @patient, :confirm => "Are you sure?", :method => :delete %>
您需要在视图中…有不同的方法来传递变量
<%= 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
问题是您没有定义路线,并且您没有提供呼叫路径