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

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

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

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

Q&A

1回答

698閲覧

編集機能を使おうとするとエラーが出ます。

TakaakiITO

総合スコア3

Ruby on Rails

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

0グッド

0クリップ

投稿2021/10/01 09:02

編集2021/10/01 09:15

前提・実現したいこと

編集機能を使えるようにしたい。

発生している問題・エラーメッセージ

ActionView::Template::Error (undefined method `name' for nil:NilClass):
6:
7: </div>
8: <h1>users#show</h1>
9: <p>名前 : <%= @user.name %></p>
10: <p>メールアドレス : <%= @user.email %></p>
11: <p>プロフィール : <%= @user.profile %></p>
12:

app/views/coffees/show.html.erb:9

<h1>Coffee詳細</h1> <div class="coffee"> <p><%= @coffee.name %></p> <p><%= @coffee.created_at %></p> <p><%= image_tag @coffee.image_url, size: "250x200" if @coffee.image? %></p> </div> <h1>users#show</h1> <p>名前 : <%= @user.name %></p> <p>メールアドレス : <%= @user.email %></p> <p>プロフィール : <%= @user.profile %></p> <% if current_user.id == @user.id %> <%= link_to "編集する", edit_user_registration_path %> <% end %> <h2>ユーザーの投稿一覧</h2> <% @user.coffees.each do |t| %> <%= t.user.name %> <%= t.body %> <% end %> <%= link_to "編集する", edit_coffee_path(@coffee.id) %> <%= link_to "Coffee一覧に戻る", coffees_path %> class CoffeesController < ApplicationController before_action :authenticate_user!, only: [:new, :create] def index @coffees=Coffee.all end def new @coffee = Coffee.new end def create coffee = Coffee.new(coffee_params) coffee.user_id = current_user.id if coffee.save redirect_to :action => "index" else redirect_to :action => "new" end end def show @coffee = Coffee.find(params[:id]) end def edit @coffee =Coffee.find(params[:id]) end def update coffee = Coffee.find(params[:id]) if coffee.update(coffee_params) redirect_to :action => "show", :id => coffee.id else redirect_to :action => "new" end end def destroy coffee = Coffee.find(params[:id]) coffee.destroy redirect_to action: :index end private def coffee_params params.require(:coffee).permit(:name, :address, :telephone, :seat, :wifi, :smoke, :image) end end ActiveRecord::Schema.define(version: 2021_09_27_150000) do create_table "coffees", force: :cascade do |t| t.string "name" t.string "address" t.string "telephone" t.string "seat" t.string "wifi" t.string "smoke" t.string "image" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "name" t.text "profile" t.string "title" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end エラーメッセージ

該当のソースコード

ruby on rails
ソースコード

### 試したこと nilクラスになっていたので、データーベースにname作ったのですが変わりませんでした。 ### 補足情報(FW/ツールのバージョンなど) 始めたばかりで分からないことが多いですが、手順を教えてくださるとうれしいです。 コードで必要なところもまだわかっていないのであったら追記します。 宜しくお願いします。

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

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

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

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

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

guest

回答1

0

コントローラとapp/views/coffees/show.html.erbを提示しないとわからないです

投稿2021/10/01 09:06

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

TakaakiITO

2021/10/01 09:13

class CoffeesController < ApplicationController before_action :authenticate_user!, only: [:new, :create] def index @coffees=Coffee.all end def new @coffee = Coffee.new end def create coffee = Coffee.new(coffee_params) coffee.user_id = current_user.id if coffee.save redirect_to :action => "index" else redirect_to :action => "new" end end def show @coffee = Coffee.find(params[:id]) end def edit @coffee =Coffee.find(params[:id]) end def update coffee = Coffee.find(params[:id]) if coffee.update(coffee_params) redirect_to :action => "show", :id => coffee.id else redirect_to :action => "new" end end def destroy coffee = Coffee.find(params[:id]) coffee.destroy redirect_to action: :index end private def coffee_params params.require(:coffee).permit(:name, :address, :telephone, :seat, :wifi, :smoke, :image) end end <h1>Coffee詳細</h1> <div class="coffee"> <p><%= @coffee.name %></p> <p><%= @coffee.created_at %></p> <p><%= image_tag @coffee.image_url, size: "250x200" if @coffee.image? %></p> </div> <h1>users#show</h1> <p>名前 : <%= @user.name %></p> <p>メールアドレス : <%= @user.email %></p> <p>プロフィール : <%= @user.profile %></p> <% if current_user.id == @user.id %> <%= link_to "編集する", edit_user_registration_path %> <% end %> <h2>ユーザーの投稿一覧</h2> <% @user.coffees.each do |t| %> <%= t.user.name %> <%= t.body %> <% end %> <%= link_to "編集する", edit_coffee_path(@coffee.id) %> <%= link_to "Coffee一覧に戻る", coffees_path %>
TakaakiITO

2021/10/01 09:14

コントローラーとビューになります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問