2016-04-06 100 views
0

我是新來的紅寶石。我想刪除記錄。但無法刪除。請檢查我的代碼 -數據沒有從sqlite3表中刪除

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

你能夠插入和更新 – uzaif

+0

是能插入並更新 – Chinmay235

+1

你確定'link_to ... method::destroy'不應該是':delete'嗎?另外,更改'def destroy'來說''RubyWinSource.find(params [:id])。destroy' –

回答

0

你放下你的數據庫rake db:drop然後耙db:migrate,以創建表

+0

這與這個問題有什麼關係? –

+0

我的數據庫工作正常。我已添加,更新,查看成功。但唯一的問題是記錄沒有被刪除。 – Chinmay235

0

做一個delete請求,而破壞了object:

<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_sourcedestroy行動得到執行看起來不錯。

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 
+0

有點像我的評論,或者來自@dkp的答案! –

+0

好的讓我檢查你的代碼 – Chinmay235

+0

沒有同樣的問題。雖然我點擊銷燬鏈接*刪除確認框*未來。並將頁面重定向到View頁面。 – Chinmay235