#エラー内容
ruby on railsのshowやeditにおけるidでのurl参照で
undefined method `set_article' for #<ArticlesController:0x00007f3face9f9b0> Did you mean? set_trace_func
といったようなエラーが出ます。
いろいろと調べた結果「モデルのid属性が渡されておらず、ActiveRecord Relationが渡されている」という記事を見つけたのですが、初心者の自分にはどうすればいいのかわかりませんでした。
対処法を教えていただきたいです。
#ファイル
articlesController
1class ArticlesController < ApplicationController 2 before_action :set_article, only: %i[ show edit update destroy ] 3 4 # GET /articles or /articles.json 5 def index 6 @articles = Article.all 7 end 8 9 # GET /articles/1 or /articles/1.json 10 def show 11 @article = Article.find(params[:id]) 12 end 13 14 # GET /articles/new 15 def new 16 @article = Article.new 17 end 18 19 # GET /articles/1/edit 20 def edit 21 @article = Article.find(params[:id]) 22 end 23 24 # POST /articles or /articles.json 25 def create 26 @article = Article.new(article_params) 27 28 respond_to do |format| 29 if @article.save 30 format.html { redirect_to @article, notice: "Article was successfully created." } 31 format.json { render :show, status: :created, location: @article } 32 else 33 format.html { render :new, status: :unprocessable_entity } 34 format.json { render json: @article.errors, status: :unprocessable_entity } 35 end 36 end 37 end 38 39 # PATCH/PUT /articles/1 or /articles/1.json 40 def update 41 @article = Article.find(params[:id]) 42 respond_to do |format| 43 if @article.update(article_params) 44 format.html { redirect_to @article, notice: "Article was successfully updated." } 45 format.json { render :show, status: :ok, location: @article } 46 else 47 format.html { render :edit, status: :unprocessable_entity } 48 format.json { render json: @article.errors, status: :unprocessable_entity } 49 end 50 end 51 end 52 53 # DELETE /articles/1 or /articles/1.json 54 def destroy 55 @article = Article.find(params[:id]) 56 @article.destroy 57 respond_to do |format| 58 format.html { redirect_to articles_url, notice: "Article was successfully destroyed." } 59 format.json { head :no_content } 60 end 61 end 62 63 private 64 # Use callbacks to share common setup or constraints between actions. 65 # def set_article 66 # @article = Article.find(params[:id]) 67 # end 68 69 # Only allow a list of trusted parameters through. 70 def article_params 71 params.require(:article).permit(:title, :body, :name, :genre, :image) 72 end 73end 74
index
1<p id="notice"><%= notice %></p> 2 3<h1>Articles</h1> 4 5<table> 6 <thead> 7 <tr> 8 <th>Id</th> 9 <th>Title</th> 10 <th>Body</th> 11 <th>Name</th> 12 <th>Genre</th> 13 <th>Image</th> 14 <th colspan="3"></th> 15 </tr> 16 </thead> 17 18 <tbody> 19 <% @articles.each do |article| %> 20 <tr> 21 <td><%= article.id %></td> 22 <td><%= article.title %></td> 23 <td><%= article.body %></td> 24 <td><%= article.name %></td> 25 <td><%= article.genre %></td> 26 <td><%= article.image.attached? ? 1 : 0 %></td> 27 <td><%= link_to 'Show', article %></td> 28 <td><%= link_to 'Edit', edit_article_path(article) %></td> 29 <td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td> 30 </tr> 31 <% end %> 32 </tbody> 33</table> 34 35<br> 36 37<%= link_to 'New Article', new_article_path %> 38
show
1<p id="notice"><%= notice %></p> 2 3<p> 4 <strong>Title:</strong> 5 <%= @article.title %> 6</p> 7 8<p> 9 <strong>Body:</strong> 10 <%= @article.body %> 11</p> 12 13<p> 14 <strong>Name:</strong> 15 <%= @article.name %> 16</p> 17 18<p> 19 <strong>Genre:</strong> 20 <%= @article.genre %> 21</p> 22 23<% if @user.image.attached? %> 24 <p> 25 <strong>Image:</strong> 26 <%= image_tag @article.image %> 27 </p> 28<% end %> 29 30<%= link_to 'Edit', edit_article_path(@article) %> | 31<%= link_to 'Back', articles_path %> 32
routes
1Rails.application.routes.draw do 2 resources :articles 3 root 'articles#index' 4 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 5end
##試したこと
routesで
get'/show/:id' => 'articles#show' get'/edit/:id' => 'articles#show'
と指定する。
index.html.erbの
<td><%= link_to 'Edit', edit_article_path(article) %></td>を <td><%= link_to 'Edit', edit_article_path(article[1]) %></td>で指定してみるなど
回答1件
あなたの回答
tips
プレビュー