質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

757閲覧

rails5.2 idのurl参照について

tomo83

総合スコア2

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/10/18 13:16

#エラー内容
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>で指定してみるなど

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2021/10/18 13:41

エラーメッセージは全部載せてください
tomo83

2021/10/18 14:27

載せていないわけではなくfuncで途切れていて書いていないのです。 画面の左上から右下まですべてコピペすると、以下のようになります NoMethodError in ArticlesController#show undefined method `set_article' for #<ArticlesController:0x00007f3fb01e9cd0> Did you mean? set_trace_func Extracted source (around line #426): 424 425 426 427 428 429 lambda do |target, value, &block| target, block, method, *arguments = expand(target, value, block) target.send(method, *arguments, &block) end end Rails.root: /app Application Trace | Framework Trace | Full Trace activesupport (5.2.6) lib/active_support/callbacks.rb:426:in `block in make_lambda' activesupport (5.2.6) lib/active_support/callbacks.rb:179:in `block (2 levels) in halting_and_conditional' actionpack (5.2.6) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>' activesupport (5.2.6) lib/active_support/callbacks.rb:180:in `block in halting_and_conditional' activesupport (5.2.6) lib/active_support/callbacks.rb:513:in `block in invoke_before' activesupport (5.2.6) lib/active_support/callbacks.rb:513:in `each' activesupport (5.2.6) lib/active_support/callbacks.rb:513:in `invoke_before' activesupport (5.2.6) lib/active_support/callbacks.rb:131:in `run_callbacks' actionpack (5.2.6) lib/abstract_controller/callbacks.rb:41:in `process_action' actionpack (5.2.6) lib/action_controller/metal/rescue.rb:22:in `process_action' actionpack (5.2.6) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action' activesupport (5.2.6) lib/active_support/notifications.rb:168:in `block in instrument' activesupport (5.2.6) lib/active_support/notifications/instrumenter.rb:23:in `instrument' activesupport (5.2.6) lib/active_support/notifications.rb:168:in `instrument' actionpack (5.2.6) lib/action_controller/metal/instrumentation.rb:32:in `process_action' actionpack (5.2.6) lib/action_controller/metal/params_wrapper.rb:256:in `process_action' activerecord (5.2.6) lib/active_record/railties/controller_runtime.rb:24:in `process_action' actionpack (5.2.6) lib/abstract_controller/base.rb:134:in `process' actionview (5.2.6) lib/action_view/rendering.rb:32:in `process' actionpack (5.2.6) lib/action_controller/metal.rb:191:in `dispatch' actionpack (5.2.6) lib/action_controller/metal.rb:252:in `dispatch' actionpack (5.2.6) lib/action_dispatch/routing/route_set.rb:52:in `dispatch' actionpack (5.2.6) lib/action_dispatch/routing/route_set.rb:34:in `serve' actionpack (5.2.6) lib/action_dispatch/journey/router.rb:52:in `block in serve' actionpack (5.2.6) lib/action_dispatch/journey/router.rb:35:in `each' actionpack (5.2.6) lib/action_dispatch/journey/router.rb:35:in `serve' actionpack (5.2.6) lib/action_dispatch/routing/route_set.rb:840:in `call' rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call' rack (2.2.3) lib/rack/etag.rb:27:in `call' rack (2.2.3) lib/rack/conditional_get.rb:27:in `call' rack (2.2.3) lib/rack/head.rb:12:in `call' actionpack (5.2.6) lib/action_dispatch/http/content_security_policy.rb:18:in `call' rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context' rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/cookies.rb:670:in `call' activerecord (5.2.6) lib/active_record/migration.rb:559:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' activesupport (5.2.6) lib/active_support/callbacks.rb:98:in `run_callbacks' actionpack (5.2.6) lib/action_dispatch/middleware/callbacks.rb:26:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call' web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app' web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call' web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch' web-console (3.7.0) lib/web_console/middleware.rb:20:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call' railties (5.2.6) lib/rails/rack/logger.rb:38:in `call_app' railties (5.2.6) lib/rails/rack/logger.rb:26:in `block in call' activesupport (5.2.6) lib/active_support/tagged_logging.rb:71:in `block in tagged' activesupport (5.2.6) lib/active_support/tagged_logging.rb:28:in `tagged' activesupport (5.2.6) lib/active_support/tagged_logging.rb:71:in `tagged' railties (5.2.6) lib/rails/rack/logger.rb:26:in `call' sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/remote_ip.rb:81:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/request_id.rb:27:in `call' rack (2.2.3) lib/rack/method_override.rb:24:in `call' rack (2.2.3) lib/rack/runtime.rb:22:in `call' activesupport (5.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (5.2.6) lib/action_dispatch/middleware/static.rb:127:in `call' rack (2.2.3) lib/rack/sendfile.rb:110:in `call' railties (5.2.6) lib/rails/engine.rb:524:in `call' puma (3.12.6) lib/puma/configuration.rb:227:in `call' puma (3.12.6) lib/puma/server.rb:706:in `handle_request' puma (3.12.6) lib/puma/server.rb:476:in `process_client' puma (3.12.6) lib/puma/server.rb:334:in `block in run' puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread' Request Parameters: {"id"=>"1"} Toggle session dump Toggle env dump Response Headers: None
guest

回答1

0

ベストアンサー

エラーの発生は show のなかでなく、Railsの深いところで発覚ですね。
とすると、、、
class ArticlesController < ApplicationController は載っているのがすべてですか?省略している所はありますか?

method set_article が見当たりません。

投稿2021/10/18 22:54

winterboum

総合スコア23416

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tomo83

2021/10/19 01:05

上の記載で全てになります。 ただ一つ原因として考えられるのが、active storageを用いている点です。 もしかしたら、dbの中の他のidが干渉してきているのではないかなと思うのですが、
winterboum

2021/10/19 01:19

いえ、 before_action で set_article を呼んでいるのに、それが定義されていないのが原因です
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問