2015-11-09 65 views
0

您好我有一個Rails應用程序,用戶可以有一個一對多的關係的時間表。Rails - 添加一個「刪除」按鈕到用戶時間表

當我加入這個

<%= link_to "delete", user, method: :delete, 
       data: { confirm: "You sure?" } %> 

到_user.html它呈現一個刪除鏈接,刪除用戶沒問題

但是當我加入這個

<%= link_to "delete", timetable, method: :delete, 
        data: { confirm: "You sure?" } %> 

到_timetable.html .erb它會引發錯誤

ActionController::RoutingError (No route matches [DELETE] "/timetable.4"): 

我的routes.rb

get 'password_resets/new' 
    get 'password_resets/edit' 
    root 'static_pages#home' 
    get 'help' => 'static_pages#help' 
    get 'about' => 'static_pages#about' 
    get 'contact' => 'static_pages#contact' 
    get 'timetable' => 'timetables#new' 
    get 'signup' => 'users#new' 
    get 'mobile' => 'users#mobile' 
    get 'login' => 'sessions#new' 
    post 'login' => 'sessions#create' 
    delete 'logout' => 'sessions#destroy' 


    resources :account_activations, only: [:edit] 
    resources :password_resets, only: [:new, :create, :edit, :update] 
    resources :timetables 
    resources :users 
    resources :projects 

時間表控制器

class TimetablesController < ApplicationController 

    before_action :logged_in_user, only: [:create, :destroy] 
    before_action :correct_user, only: :destroy 

    def index 
    @timetables = current_user.timetables.find(current_user) 
    end 

    def new 
    @user = User.find(current_user) 
    @timetable = Timetable.new 
    @timetables = @user.timetables.paginate(page: params[:page], per_page: 1) 
    end 

    def feed 
    Timetable.where("user_id = ?", id) 
    end 

    def show 
    @feed_items3 = current_user.feed.paginate(page: params[:page]) 
    @timetable = current_user.timetables.find(params[:id]) 
    end 

    def create 
    @timetable = Timetable.new(timetable_params) 
    @user = User.find(current_user) 

    if current_user.timetables.create(timetable_params) 
     flash[:success] = "Timetable created!" 
     redirect_to timetable_path 
    else 
     flash[:success] = "Timetable not created!" 
     redirect_to timetable_path 
    end 
    end 

    def destroy 
    @timetable = Timetable.find(params[:id]) 
    @timetable.destroy 
    redirect_to timetables_path, notice: "The timetable #{@timetable.name} has been deleted." 
    end 

    private 
    def timetable_params 
    params.require(:timetable).permit(:name, :attachment, :id) 
    end 

    def correct_user 
    @project = current_user.projects.find_by(id: params[:id]) 
    redirect_to root_url if @project.nil? 
    end 

end 

回答

1

沒有看到你的控制器就很難確定,但我會看幾個方面:

指定路徑&要刪除的時間表對象:

<%= link_to "Delete Timetable", timetable_path(timetable), :method => :delete %> 

確保了時間表的破壞作用坐在timetable_controller


關於你的routes.rb文件中我不知道爲什麼你需要:

get 'timetable' => 'timetables#new'

,然後還

resources :timetables

這是重複的 - 爲什麼不試試 '只有' 使用(編輯您的喜好):

resources :timetables, only: [:index, :new, :create, :destroy] 

相關問題