Shelalegen
Shelalegen

Reputation: 21

No route matches [POST] "/projects/16/todos/35/edit"

I am Receiving this error

No route matches [POST] "/projects/16/todos/35/edit"

I understand that I don't have to put a method for that, but how can I add it

This is routes.rd

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  get 'projects/index'
  post 'projects/new'

  resources :projects do
    resources :todos
end
  root 'projects#index'
end

This is edit.html.erb (for a task)

<h1>Edit Task</h1>

<%= form_for :todo, local: true do |form| %>
  <p>

    <%= form.label :body %><br>
    <%= form.text_field :body %>
  </p>

  <div>
<%= form.submit%>
  </div>
<% end %>

This is todos_controller.rb

class TodosController < ApplicationController

  def new
    @project = Project.find(params[:project_id])
  end

  def create
    @project = Project.find(params[:project_id])
    @todo = @project.todos.create(todos_params)
    redirect_to projects_path
  end

  def edit
    @project = Project.find(params[:project_id])
    @todo = @project.todos.find(params[:id])
  end

  def update
    @project = Project.find(params[:project_id])
    @todo = @project.todos.find(params[:id])
    @todo.save
  end

def destroy
  @project = Project.find(params[:project_id])
  @todo = @project.todos.find(params[:id])
  @todo.destroy
  redirect_to projects_path
end

  private
    def todos_params
      params.require(:todo).permit(:body)
    end
end

Please help to fix this problem

Upvotes: 0

Views: 390

Answers (2)

Satishakumar Awati
Satishakumar Awati

Reputation: 3798

As you can see from here, the request fired is POST request, instead it should be a GET request

Receiving this error No route matches [POST] "/projects/16/todos/35/edit"

Make sure to fire GET request when you click on Edit TODO.

Defined routes with

resources :projects do
    resources :todos
end

are

 project_todos GET    /projects/:project_id/todos(.:format)          todos#index
                  POST   /projects/:project_id/todos(.:format)          todos#create
 new_project_todo GET    /projects/:project_id/todos/new(.:format)      todos#new
edit_project_todo GET    /projects/:project_id/todos/:id/edit(.:format) todos#edit
     project_todo GET    /projects/:project_id/todos/:id(.:format)      todos#show
                  PUT    /projects/:project_id/todos/:id(.:format)      todos#update
                  DELETE /projects/:project_id/todos/:id(.:format)      todos#destroy

So you can make use of rails view helper edit_project_todo(project, todo) in link_to for Edit TODO to fire GET request.

Upvotes: 1

Anand
Anand

Reputation: 6531

in your edit.html.erb

<h1>Edit Task</h1>

<%= form_for @todo, :url => project_todo_path(@project,@todo), :html => { class: "your_class", :method => "put" } do |form| %>
  <p>
    <%= form.label :body %><br>
    <%= form.text_field :body %>
  </p>

  <div>
    <%= form.submit%>
  </div>
<% end %>

Note: - form_for is used for any object and and here your object is @todo not todo and by default form_for hits create action if @todo is of new and if @todo is existing record then it will hit update action, here explicitly you can provide url to which action is will hit after submitting form . so here i have manually provided action of update and also provided method: :put because by default form is of POST type request. hopefully it will clear you doubts.

Upvotes: 0

Related Questions