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

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

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

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

Q&A

1回答

220閲覧

Couldn't find Image with 'id'=edit エラー

asdfghjkl123

総合スコア1

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/05/27 15:14

編集2021/05/28 04:28

イメージ説明初心者です。
フリマアプリを作っています。商品の編集更新画面です。
エラーが解決できません…
よろしくお願い致します。

ルーティング

ruby

1Rails.application.routes.draw do 2 3 get "/images", to: "images#index", as: :index 4 5 get "/images/new", to: "images#new", as: :new_image 6 post "/images/new", to: "images#create" 7 8 get "/images/(:id)/edit", to: "images#edit", as: :edit_image 9 patch "/images/(:id)", to: "images#update" 10 11 get "/images/(:id)", to: "images#show" 12end 13``` 14 15コントローラ 16--- 17```ruby on rails 18class ImagesController < ApplicationController 19 layout "logged_in" 20 21 def new 22 @title = "新規投稿" 23 @image = Image.new 24 end 25 26 def show 27 @title = "投稿の詳細" 28 end 29 30 def create 31 @image = Image.new(image_params) 32 @image.user_id = current_user.id 33 34 if !@image.save 35 flash[:danger] = "投稿の追加に失敗しました。" 36 @title = "新規投稿" 37 render :new and return 38 end 39 flash[:success] = "投稿を追加しました" 40 redirect_to action: :index 41 end 42 43 def index 44 @title = "出品投稿一覧" 45 46 @user = current_user 47 @images = @user.images 48 end 49 50 def edit 51 @title = "商品情報の編集" 52 @image = Image.find(params[:id]) 53 end 54 55 def update 56 @image = Image.find(params[:id]) 57 @image.assign_attributes(image_params) 58 if !@image.save 59 flash.now[:danger] = "投稿の更新に失敗しました" 60 @title = "投稿の編集" 61 render :edit and return 62 end 63 flash[:success] = "投稿を更新しました" 64 redirect_to action: :index 65 end 66 67 68 private 69 70 def image_params 71 params.require(:image).permit( 72 :name, :description, :price, :image 73 ) 74 end 75 76 def redirect_to_login_unless_logged_in 77 redirect_to static_path if current_user.nil? 78 end 79 80 def current_user 81 User.find(session[:user_id]) if session[:user_id] 82 end 83end 84 85``` 86 87ビュー(edit) 88--- 89```Ruby on rails 90<% content_for :title, @title %> 91 92<h2 class="title"><%= @title %></h2> 93<h3>商品追加フォーム</h3><br> 94 95<%= form_for @image, url: edit_image_path do |form| %> 96 <div> 97 <%= form.label :name, "商品名:" %><br> 98 <%= form.text_field :name %><br> 99 <%= form.label :description, "商品説明:" %><br> 100 <%= form.text_area :description, size: "30x10" %><br> 101 <%= form.label :price, "価格:" %><br> 102 <%= form.text_field :price %><br> 103 <%= form.label :image, "画像を選択:" %> 104 <%= form.file_field :image %> 105 </div> 106 <%= form.submit "投稿を更新" %> 107<% end %> 108 109``` 110 111ビュー(index) 112--- 113```Ruby on rails 114 115<% content_for :title, @title %> 116 117<h2 class="title"><%= @title %></h2> 118 119<div class="link"> 120 <%= link_to "新規投稿", new_image_path %> 121</div> 122 123<p><%= @user.name %>さん、こんにちは!</p> 124 125<ul> 126 <% @images.each do |image| %> 127 <li><%= image.user.name %>:<%= image.name %> (<%= image.created_at %>)</li> 128 <li><%= image.image %> (<%= image.created_at %>)</li> 129 <li><%= image.description %> (<%= image.created_at %>)</li> 130 [<%= link_to "編集", edit_image_path(image) %>] 131 <% end %> 132 <% if @images.empty? %> 133 <li>出品している商品はありません。</li> 134 <% end %> 135</ul> 136 137```

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

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

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

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

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

winterboum

2021/05/27 22:50

code読みにくいので、code全体を ```(半角逆コーテーション3連続)だけの行で挟んでください。 エラーメッセージはそれだけではなく、どのファイルのどの行で起きたかもあるかと。全体を載せてください。 どういう操作をしたときに起きたのかを載せてください
guest

回答1

0

どういう操作で起きたのか、も書いていただかないと、なにが起きたのかをcodeを読んで考えなければならず、回答者の負荷がたかくなります。

form_for @image, url: edit_image_path
ですが、url: optionが不要でかつ正しくないのが原因かも
form_for @image, url: edit_image_path(@image)

form_for @image
で試してください。

投稿2021/05/28 04:42

winterboum

総合スコア23416

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問