2

I am following along with Railscast 035 Custom REST Actions. I have everything working locally, but when I pushed to heroku, I am getting a page does not exist error for /tasks/2/complete Heroku App GitHub Repo

When I click on complete task link, I should create a put request to the rails app's tasks#complete action. Instead I am getting a 404 error. In my logs I have the following

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>

Controller

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

routes

resources :tasks do
  get 'completed', on: :collection
  put 'complete', on: :member
end

I don't think you need a view for that action because it redirects to the completed_tasks_path

1 Answer 1

0

Try changing from put to patch and seeing if that fixes it?

I tried the app, recreated the error, but can't see any problems with your code. Maybe you could also try this:

#config/routes.rb
resources :tasks do
    member do
        patch :complete
    end
end
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.