簡単なCRUD処理を実装したアプリケーションを作成して練習しています。
indexの画面で削除機能は実装できたのですが、"show"の画面で同じコードを編集しても実装されずエラーが出てしまいます。
<%= link_to '削除', task_path(task.id), method: :delete, class: "btn-sm btn-danger", data: { confirm: "削除してよろしいですか?", commit: "削除する", cancel: "やめる", title: "ご確認ください" } %>
task_path(task.id)の部分で間違っていると思うのですが、どのように編集すれば解決できないです。一緒にみてもらえないでしょうか。
以下、編集した内容です。
show.html.erb
<strong>title : </strong> <%= @task.title %> </p> <p> <strong>content :</strong> <%= @task.content %> </p> <%= link_to '戻る',tasks_path, class: "btn btn-light" %> <%= link_to '削除', task_path(task.id), method: :delete, class: "btn-sm btn-danger", data: { confirm: "削除してよろしいですか?", commit: "削除する", cancel: "やめる", title: "ご確認ください" } %>
index.html.erb
<h1>タスク一覧</h1> <ul> <% @tasks.each do |task| %> <li> title: <%= task.title %> </li> <li> content: <%= task.content %> </li> <%= link_to '詳細', task_path(task) %> <%= link_to '編集', edit_task_path(task), class: "btn-sm btn-primary" %> <%= link_to '削除', task_path(task.id), method: :delete, class: "btn-sm btn-danger", data: { confirm: "削除してよろしいですか?", commit: "削除する", cancel: "やめる", title: "ご確認ください" } %> <% end %> </ul>
tasks_controller.rb
class TasksController < ApplicationController def index @tasks = Task.all end def new @task = Task.new end def show @task = Task.find(params[:id]) end def edit @task = Task.find(params[:id]) end def create @task = Task.new(tasks_param) #@task = Task.new(title: params[:task][:title], content: params[:task][:content]) if @task.save redirect_to tasks_url else render :new end end def update @task = Task.find(params[:id]) if @task.update(tasks_param) redirect_to tasks_path else render :edit end end def destroy @task = Task.find(params[:id]) @task.destroy! redirect_to tasks_path end private def tasks_param params.require(:task).permit(:title, :content) end end
routes.rb
Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :tasks root to: 'tasks#index' end
お願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/18 11:37
2019/01/18 11:59
2019/01/18 13:50