我正在关注Railscast 035自定义REST操作。我有一切在本地工作,但是当我推送到heroku时,我得到一个页面不存在错误/tasks/2/complete
Heroku App GitHub Repo
当我点击complete task
链接时,我应该为rails应用的put
操作创建一个tasks#complete
请求。相反,我收到404错误。在我的日志中,我有以下
method=GET path=/task/2/complete host=custom-rest-actions.herokuapp.com
request_id=16761046-c4fc-4164-b0bb-f6ed6191d71a fwd="98.207.180.92" dyno=web.1
connect=7ms service=19ms status=404 bytes=1829
heroku run rake routes
Prefix Verb URI Pattern Controller#Action
root GET / tasks#index
completed_tasks GET /tasks/completed(.:format) tasks#completed
complete_task PUT /tasks/:id/complete(.:format) tasks#complete
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
Index.html.erb
<table>
<tbody>
<% @tasks.each do |task| %>
<tr>
<td><%= link_to task.name, task %></td>
<td>| <%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Complete task', complete_task_path(task), method: :put %></td>
</tr>
<% end %>
</tbody>
</table>
控制器
def completed
@tasks = Task.where(completed: true)
end
def complete
@task.update_attribute :completed, true
flash[:notice] = 'Task Completed'
redirect_to completed_tasks_path
end
路由
resources :tasks do
get 'completed', on: :collection
put 'complete', on: :member
end
我认为您不需要该操作的视图,因为它重定向到completed_tasks_path
答案 0 :(得分:0)
尝试从put
更改为patch
,看看是否可以修复它?
我尝试了该应用,重新创建了错误,但无法看到您的代码出现任何问题。也许你也可以试试这个:
#config/routes.rb
resources :tasks do
member do
patch :complete
end
end