2014-01-17 36 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

相关问题