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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

700閲覧

Rails updateできない

Meitoku

総合スコア44

Ruby on Rails 5

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/06 06:01

編集2020/11/06 07:58

ユーザーのマイページ編集機能を実装しており、管理者は他ユーザーのマイページも編集できるようにします
最初は自分のマイページだけ編集できるようにするため以下で実装しました

@mypage = Mypage.find_by(user_id: current_user.id)

今度は管理者は他ユーザーのマイページも編集できるようにするため以下のようにすると、中身が空になってしまいNomethodエラーが発生してしまいました

@mypage = Mypage.find_by(user_id: params[:id])

idが送れていないことが原因だと思いますが、idはフォームやリンクで送っているつもりですが、なぜ取得できないのでしょうか?
よろしくお願いします

##各ファイル
#####route.rb

resources :mypages,only:[:show,:edit,:new,:create,:update,:index] do member do get :my_tweets,:my_analyses,:my_forecasts end end

#####mypages.controller.rb
updateでエラー

class MypagesController < ApplicationController # before_action :search_mypage,except:[:new,:create,:update] before_action :authenticate_user! def show @user = User.find(params[:id]) @mypages = @user.mypages @myEntry=Entry.where(user_id: current_user.id) @userEntry=Entry.where(user_id: @user.id) if @user.id == current_user.id else @myEntry.each do |cu| @userEntry.each do |u| if cu.room_id == u.room_id then @isRoom = true @roomId = cu.room_id end end end if @isRoom != true @room = Room.new @entry = Entry.new end end end def new @mypage = Mypage.new end def create @mypage = Mypage.create(mypage_params) if @mypage.save redirect_to "/mypages/#{current_user.id}" else render "new" end end def edit @mypage = Mypage.find_by(user_id: params[:id]) end def update @mypage = Mypage.find_by(user_id: params[:id]) #ここで@mypageの中身が空になりNomethodエラー @mypage.update(mypage_params) if @mypage.user_id == current_user.id || current_user.admin redirect_to "/mypages/#{@mypage.id}" end def my_tweets @tweets = Tweet.where(user_id: params[:id]) end def my_analyses @analyses = Analysis.where(user_id: params[:id]) end def my_forecasts @forecasts = Forecast.where(user_id: params[:id]) end private def mypage_params params.require(:mypage).permit(:text,:prefectures).merge(user_id: current_user.id) end end

#####マイページの編集ページへ飛ぶリンク

<%= link_to "トップページへ戻る",root_path %> <div class="myname"> <h2><%= "#{@user.nickname}さんのプロフィール"%><h2> </div> <%= render "users/stats" %> <% if @user.id == current_user.id && @mypages.blank? %> <%= link_to "プロフィール作成",new_mypage_path %> <% elsif @user.id == current_user.id || current_user.admin %> <%= link_to "プロフィール編集",edit_mypage_path(@user) %> <% end %> </div class="myprof"> <% @mypages.each do |mypage| %> <div class="residence"> <p>住んでる場所<p> <br> <%=mypage.prefectures %> </div> <div class="text-myname"> <p>自己紹介</p> <br> <%=mypage.text %> </div> <div class="my-links"> <%= link_to "投稿した試合記事","#{mypage.user_id}/my_tweets",class: "my-link" %> <%= link_to "投稿した戦力分析","#{mypage.user_id}/my_analyses",class: "my-link" %> <%= link_to "投稿した試合予想","#{mypage.user_id}/my_forecasts",class: "my-link" %> </div> <%end%> </div> <% if @user.id != current_user.id %> <% if @isRoom == true %> <% if current_user.following?(@user) %> <div class="dm"> <%= link_to 'ダイレクトメッセージをする', room_path(@roomId) %> </div> <% end %> <% else %> <%= form_for @room do |f| %> <%= fields_for @entry do |e|%> <% e.hidden_field :user_id, value: @user.id %> <% end %> <%= f.submit "DMを開始する"%> <% end %> <% end %> <% end %> </div> <%= render "users/follow_form" %>

#####他ユーザーのマイページへ飛ぶリンク

<div class="game-article"> <div class="choose-btn"> <% if current_user.id == @tweet.user_id || current_user.admin? %> <%= link_to "記事を削除する","/tweets/#{@tweet.id}",method: :delete,data: { confirm: '削除しますか?' },class: "delete-article"%> <%= link_to "記事を編集する","/tweets/#{@tweet.id}/edit",method: :get,class: "edit-article" %> <% end %> <%= link_to "記事一覧へ戻る",tweets_path,class: "return-top"%> </div> <div class="user_name"> <h4>投稿者 <%= link_to "#{@tweet.user.nickname}","/mypages/#{@tweet.user_id}"%></h4> </div> <div class="game_result"> <%=@tweet.school_a.name%> <%=@tweet.school_a_score%> <a>-</a> <%=@tweet.school_b_score%> <%=@tweet.school_b.name%> </div> <div class="sub_title"> <%=@tweet.title_info%> </div> <div class=texts> <%= simple_format(@tweet.text)%> </div> <div class="image"> <%= image_tag "#{@tweet.image}"%> <% if current_user.already_liked?(@tweet)%> <%= button_to "#{@tweet.likes.count}いいね",tweet_like_path(@tweet),class: "good-after",method: :delete%> <% else %> <%= button_to "#{@tweet.likes.count}いいね",tweet_likes_path(@tweet),{class: "good"}%> <% end %> <h2>いいねしたユーザー</h2> <% @tweet.liked_users.each do |user|%> <li><%=user.nickname%></li> <% end %> </div> </div> <div class="comments"> <h4><コメント一覧></h4> <% if @comments %> <% @comments.each do |comment| %> <p> <strong><%= comment.user.nickname %>:</strong> <%= simple_format(comment.text) %> <% if user_signed_in? && current_user.id == comment.user_id || current_user.admin %> <%= link_to '削除', "/tweets/#{@tweet.id}/comments/#{comment.id}", method: :delete,class: "comment_delete_button" %> <% end %> <div class="comment_at"> <%= comment.created_at.strftime("%Y年%m月%d日 %H時%M分")%> </div> </p> <% end %> <% end %> </div> <div class="comment_container"> <!-- ここからフォーム --> <% if current_user %> <%= form_with(model:[@tweets, @comment], method: :post) do |f| %> <textarea cols="30" name="text" placeholder="コメントを記述" rows="2"></textarea> <input type="submit" value="SENT" class="game_record"> <% end %> <% end %> </div>

#####edit.html.erb

<div class="contents row"> <div class="container"> <%= form_with model: @mypage,local: true do |f|%> <%= f.select :prefectures,options_for_select(Mypage.prefectures.keys,selected: @mypage.prefectures),{prompt: "選択してください"},class: "form-control btn btn-info"%> <%= f.text_area :text, cols: 30 ,rows: 10,value: "#{@mypage.text}"%> <%=f.submit value: "SENT",class: "game_record"%> <div class="residence"> </div> <% end %> </div> </div>

エラー詳細
エラー

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

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

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

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

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

guest

回答2

0

ベストアンサー

edit.html.erb では <%= form_with model: @mypage と成っています。
するとここから返される parms[:id]はmypageのidでuserのidではないです
def update @mypage = Mypage.find_by(id: params[:id])
です

投稿2020/11/06 09:05

winterboum

総合スコア23376

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

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

Meitoku

2020/11/06 09:32

ありがとうございました!
guest

0

edit_mypage_path に 引数が渡っていないので、idは送られていません。
@userが正しいなら
edit_mypage_path(@user) です。
@userが正しいかどうかは、全体を見せていただかないとわかりません

投稿2020/11/06 06:27

winterboum

総合スコア23376

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

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

Meitoku

2020/11/06 07:55

回答ありがとうございます 記載しました @user.nicknameは表示されるので@userは正しいと思いますが、エラーが変わりません editページでbinding.pryで中身を見ると@mypageの中身は正しいのですが、update直前で@mypageを見るとnilになっています
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問