- 设定路由
resources :workers do
collection do
post :qiehuan
end
end
2.在controller内定义一个切换的方法
def qiehuan
worker = Worker.find(params[:id])
if worker.state == "在职"
worker.update(state: "离职")
else
worker.update(state: "在职")
end
redirect_to workers_path
end
——————————————
简写的方式:
def qiehuan
worker = Worker.find(params[:id])
worker.state == "在职" ? worker.update(state: "离职") : worker.update(state: "在职")
redirect_to workers_path
end
3.view页面设定按钮。切换的路径到rake routes 查看
<td><%=link_to "切换", qiehuan_workers_path({:id => worker.id}),method: :post%></td>