我是红宝石的新手。我想删除记录。但无法删除。请检查我的代码 -
ruby_win_sources / index.html.erb
<table border="1" cellpadding="1" cellspacing="1" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Author</th>
<th>Url</th>
<th colspan="3">Action</th>
</tr>
</thead>
<tbody>
<% @ruby_win_sources.each do |ruby_win_source| %>
<tr>
<td><%= ruby_win_source.name %></td>
<td><%= ruby_win_source.author %></td>
<td><%= ruby_win_source.url %></td>
<td><%= link_to 'Show', ruby_win_source %></td>
<td><%= link_to 'Edit', edit_ruby_win_source_path(ruby_win_source) %></td>
<td><%= link_to 'Destroy', ruby_win_source, method: :destroy, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
ruby_win_sources_controller.rb
class RubyWinSourcesController < ApplicationController
before_action :set_ruby_win_source, only: [:show, :edit, :update, :destroy]
# GET /ruby_win_sources
# GET /ruby_win_sources.json
def index
@ruby_win_sources = RubyWinSource.all
end
# GET /ruby_win_sources/1
# GET /ruby_win_sources/1.json
def show
end
# GET /ruby_win_sources/new
def new
@ruby_win_source = RubyWinSource.new
end
# GET /ruby_win_sources/1/edit
def edit
end
# POST /ruby_win_sources
# POST /ruby_win_sources.json
def create
@ruby_win_source = RubyWinSource.new(ruby_win_source_params)
respond_to do |format|
if @ruby_win_source.save
format.html { redirect_to @ruby_win_source, notice: 'Ruby win source was successfully created.' }
format.json { render :show, status: :created, location: @ruby_win_source }
else
format.html { render :new }
format.json { render json: @ruby_win_source.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /ruby_win_sources/1
# PATCH/PUT /ruby_win_sources/1.json
def update
respond_to do |format|
if @ruby_win_source.update(ruby_win_source_params)
format.html { redirect_to @ruby_win_source, notice: 'Ruby win source was successfully updated.' }
format.json { render :show, status: :ok, location: @ruby_win_source }
else
format.html { render :edit }
format.json { render json: @ruby_win_source.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ruby_win_sources/1
# DELETE /ruby_win_sources/1.json
def destroy
@ruby_win_source.destroy
respond_to do |format|
format.html { redirect_to ruby_win_sources_url, notice: 'Ruby win source was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ruby_win_source
@ruby_win_source = RubyWinSource.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ruby_win_source_params
params.require(:ruby_win_source).permit(:name, :author, :url)
end
end
删除确认框未打开。
请帮帮我
编辑 - 的的routes.rb
Rails.application.routes.draw do
resources :ruby_win_sources
end
答案 0 :(得分:0)
删除数据库rake db:drop
,然后选择db:migrate
以创建表格
答案 1 :(得分:0)
在销毁对象时发出delete
请求:
<td><%= link_to 'Destroy', ruby_win_source, method: :delete, data: { confirm: 'Are you sure?' } %></td>
您正在设置
,因此控制器看起来不错before_action :set_ruby_win_source, only: [:show, :edit, :update, :destroy]
因此,在执行@ruby_win_source
操作之前,您已经拥有destroy
。
答案 2 :(得分:0)
我认为您要删除的link_to不正确,应该是这样的:
<%= link_to 'Destroy', ruby_win_source, method: :delete, data: { confirm: 'Are you sure?' } %>
然后将控制器destroy
方法更新为:
# DELETE /ruby_win_sources/1
# DELETE /ruby_win_sources/1.json
def destroy
RubyWinSource.find_by_id(params[:id]).destroy
respond_to do |format|
format.html { redirect_to ruby_win_sources_url, notice: 'Ruby win source was successfully destroyed.' }
format.json { head :no_content }
end
end