2014-01-17 33 views
1

所以我有關於電影的這個數據庫。我爲它創建了一個resources :movies條目進入我的路由文件,這應該爲它提供所需的每個路由。嘗試更新數據庫條目時路由錯誤

這是我的電影控制器:https://github.com/Veske/form/blob/ryhm/app/controllers/movies_controller.rb

這是我的錯誤,當我嘗試在whatever.com/movies/1/edit編輯影片:

Routing Error 
No route matches [POST] "/movies/1/edit" 

這些都是電影路線現在:

movies_path GET  /movies(.:format) movies#index 
POST  /movies(.:format) movies#create 
new_movie_path GET  /movies/new(.:format) movies#new 
edit_movie_path GET  /movies/:id/edit(.:format) movies#edit 
movie_path GET  /movies/:id(.:format) movies#show 
PATCH /movies/:id(.:format) movies#update 
PUT /movies/:id(.:format) movies#update 
DELETE /movies/:id(.:format) movies#destroy 

現在人們清楚地看到,的確有電影沒有POST /:現在ID /編輯。但爲什麼呢?我輸入的路線不應該給我嗎?

編輯:

鏈接到電影查看:https://github.com/Veske/form/blob/ryhm/app/views/movies/edit.html.erb

回答

1

您的問題是,我們在調用編輯頁面上的窗體上的POST,因此爲什麼你得到錯誤No route matches [POST] "/movies/1/edit"

沒有看到你的代碼,這只是一個猜測,但無論是:

  1. 的形式將需要更改爲使用PUT方法。
  2. 進入編輯狀態的鏈接很糟糕,需要成爲GET請求。
+0

明白了,試圖現在修復它.. – Veske

+0

好吧我使用PUT:<%= form_for:movie,:html => {:method =>:put} do | f | %> 但是這並沒有改變我沒有爲電影/:id/edit放置路線的事實。我怎樣才能獲得這條路線?我的意思是......資源應該爲我做這件事? – Veske

+0

在您的控制器中,在編輯動作中,在一個實例變量(例如'@movie = Movie.find(params [:id])')中加載電影,然後執行'form_for @ movie' – CDub

1

MoviesController需求來設定影片的編輯動作

def edit 
    @movie = Movie.find(params[:id]) 
end 

或者更好的是,幹出你的代碼,並創建一個before_action,將其設置爲您的寧靜路線

before_action :set_movie, only: [:show, :edit, :update, :destroy] 

def set_movie 
    @movie = Movie.find(params[:id]) 
end 
+0

耙路線仍然沒有顯示電影PUT路線/ 1 /然後編輯。 – Veske

+0

也嘗試了其他版本,但由於某些原因,它仍然不想爲其製作路線。 – Veske

+0

您應該不需要在表單上指定方法,因爲Rails會自動處理該方法。什麼是檢查到您的回購權現在的電影/ edit.html.erb應該工作 – Dhaulagiri

相關問題