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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

5237閲覧

ArgumentError in Users#showを解決したい

kousuke24

総合スコア29

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2019/09/26 14:59

現在Ruby on Railsでアプリケーションを作成しています。<% form_for ~ でエラーが出てしまい詰まっている状況です。

エラー内容

ArgumentError in Users#show First argument in form cannot contain nil or be empty

エラー箇所

<div class="offset-lg-2 col-lg-4 text-center profile-image mb-3"> <%= form_for(@user) do |f| %> <% if @user.image? %> <%= image_tag @user.image.thumb.url, class: "round-img" %> <% else %>

コントローラー

class UsersController < ApplicationController def show @user = User.find_by(id: params[:id]) end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if current_user == @user if @user.update(user_params) flash[:success] = 'プロフィール画像を変更しました' render :show else flash.now[:danger] = 'プロフィール画像の変更に失敗しました' render :show end else redirect_to user_path end end private def user_params params.require(:user).permit(:image) end end

ビュー

<div class="container"> <h3 class="text-center text-secondary mb-4">ユーザー情報</h3> <div class="row col-lg-6 offset-lg-3"> <div class="offset-lg-2 col-lg-4 text-center profile-image mb-3"> <%= form_for(@user) do |f| %> <% if @user.image? %> <%= image_tag @user.image.thumb.url, class: "round-img" %> <% else %> <%= image_tag "/assets/default.jpg", class: "round-img" %> <% end %> <% if @user == current_user %> <button type="button" class="btn btn-outline-secondary rounded-pill mt-3"> <%= f.file_field :image, accept: 'image/jpg,image/gif,image/png' %> </button> <%= f.submit 'プロフィール画像変更', class: 'btn btn-secondary mt-3' %> <% end %> <% end %> </div> <div class="col-lg-6 text-center profile-text mb-3"> <h3><%= @user.name %></h3> <% if @user == current_user %> <p><%= @user.email %></p> <%= link_to "プロフィール編集", edit_user_registration_path, class: "btn btn-outline-dark common-btn edit-profile-btn" %> <% end %> </div> </div> </div>

ルーティング

Rails.application.routes.draw do root to: 'tops#index' get '/users/:id', to: 'users#show', as: 'user' patch '/users/:id', to: 'users#update' resources :posts, only: %i(new create show destroy) do resources :photos, only: %i(create) resources :likes, only: %i(create destroy) end devise_for :users, controllers: { registrations: 'registrations', omniauth_callbacks: 'users/omniauth_callbacks' } # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end

form_forでnilもしくは空の状態でエラーが出ていると思うのですが、解決する方法がわかりません。ご教授頂ければ幸いです。よろしくお願いします。

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

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

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

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

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

guest

回答2

0

自己解決

Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'registrations', omniauth_callbacks: 'users/omniauth_callbacks' } root to: 'tops#index' get '/users/:id', to: 'users#show', as: 'user' patch '/users/:id', to: 'users#update' resources :posts, only: %i(new create show destroy) do resources :photos, only: %i(create) resources :likes, only: %i(create destroy) end # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end

ルーティングの devise_for :users,
controllers: { registrations: 'registrations', omniauth_callbacks: 'users/omniauth_callbacks' }
を一番前に持ってきたら解決しました。

投稿2019/09/26 23:54

kousuke24

総合スコア29

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

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

0

@user = User.find_by(id: params[:id])
で、存在していない id を指定しているからです。

show に飛ぶview(とそのcntrollerもかも)に問題がありそうです

投稿2019/09/26 15:49

winterboum

総合スコア23284

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問