idを含んだルーティングエラーです。ルーティングの書き方は間違っていないと思うのですが、<%= link_to "url"%>の部分の記述が間違っているのでしょうか?初歩的な質問ですがよろしくお願いします。
<index.html.erb>
<h1>TodoApp</h1> <%=link_to 'CREATE TASK', '/tasks/new', class:"btn btn-primary" %> <h2>[TASK LIST]</h2> <% @tasks.each do |task| %> <p>・<%=task.title %> <%=link_to '/tasks/' + task.id.to_s, method: :delete do %> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16"> <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/> <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4L4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/> </svg> <%= link_to "edit", "/tasks/#{@task.id}/edit" %> ##### <% end %> </p> <% end %>
<routes.rb>
Rails.application.routes.draw do get 'tasks' => 'tasks#index' get 'tasks/new' => 'tasks#new' post 'tasks/create' => 'tasks#create' delete 'tasks/:id' => 'tasks#destroy' get 'tasks/:id/edit' => 'tasks#edit' ##### post 'tasks/:id/update' => 'tasks#update' # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end
<edit.html.erb>
<%= form_with url: "/tasks/@task.id/update", local:true do |f| %> <p><%= f.text_field :title, class: "form-control" %></p> <p><%= f.submit "edit", class: "btn btn-outline-primary" %></p> <% end %>
<tasks.controller.rb>
class TasksController < ApplicationController def index @tasks = Task.all end def new end def create @task = Task.create(title: params[:title]) redirect_to "/tasks" end def destroy @task = Task.find(params[:id]) @task.destroy redirect_to "/tasks" end def edit ####### @task = Task.find(params[:id]) binding.pry end def update @task = Task.find(params[:id]) @task.update(title: params[:title]) redirect_to "/tasks" end end
回答2件
あなたの回答
tips
プレビュー