Home>

It implements the user's My Page editing function and allows the administrator to edit other users' My Page as well.
At first, I implemented it below so that I can edit only my page

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

This time, when the administrator did the following to allow other users' My Page to be edited, the contents became empty and a No method error occurred.

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


I think the cause is that the id was not sent, but I intend to send the id by form or link, why can't I get it?
Thank you

Each file 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

Error in update

class MypagesController</pre>
Link to the edit page of My Page
<pre><code><% = link_to "Return to top page", root_path%><% = "# {@ user.nickname}'s profile"%><% = render "users/stats"%><% if @ user.id == current_user.id&&@ mypages.blank?%><% = link_to "Create profile", new_mypage_path%><% elsif @ user.id == current_user.id || current_user.admin%><% = link_to "Edit profile", edit_mypage_path (@user)%><% end%><% @ mypages.each do | mypage |%>      <p>Where you live<p>  <br>  <% = mypage.prefectures%>          <p>Self-introduction</p>  <br>  <% = mypage.text%>          <% = link_to "Posted Match Articles", "# {mypage.user_id}/my_tweets", class: "my-link"%>  <% = link_to "Posted Force Analysis", "# {mypage.user_id}/my_analyses", class: "my-link"%>  <% = link_to "Posted Match Prediction", "# {mypage.user_id}/my_forecasts", class: "my-link"%>  <% end%><% if @ user.id! = current_user.id%><% if @isRoom == true%>  <% if current_user.following? (@User)%>            <% = link_to'send a direct message', room_path (@roomId)%>         <% end%><% else%>  <% = form_for @room do | f |%>    <% = fields_for @entry do | e |%>      <% e.hidden_field: user_id, value: @ user.id%>    <% end%>    <% = f.submit "Start DM"%>  <% end%><% end%><% end%><% = render "users/follow_form"%>
A link to jump to another user's My Page
  <% if current_user.id == @ tweet.user_id || current_user.admin?%><% = link_to "Delete article", "/ tweets/# {@ tweet.id}", method:: delete, data: {confirm:'Are you sure I want to delete? '},

class: "delete-article"%><% = link_to "Edit article", "/ tweets/# {@ tweet.id}/edit", method:: get, class: "edit-article"%><% end%><% = link_to "Return to article list", tweets_path, class: "return-top"%>       Posted by<% = link_to "# {@ tweet.user.nickname}", "/ mypages/# {@ tweet.user_id}"%>     <% [email protected]_a.name%><% [email protected]_a_score%>  -  <% [email protected]_b_score%><% [email protected]_b.name%>    <% [email protected]_info%>    <% = simple_format (@ tweet.text)%>   <% = image_tag "# {@ tweet.image}"%><% if current_user.already_liked? (@Tweet)%>  <% = button_to "# {@ tweet.likes.count} Like", tweet_like_path (@tweet), class: "good-after", method:: delete%><% else%>  <% = button_to "# {@ tweet.likes.count} Likes", tweet_likes_path (@tweet), {class: "good"}%><% end%>   Liked users   <% @ tweet.liked_users.each do | user |%>  <li><% = user.nickname%></li><% end%>         <Comment list>   <% if @comments%>    <% @ comment.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'Delete', "/ tweets/# {@ tweet.id}/comments/# {comment.id}", method:: delete, class: "comment_delete_button"%>          <% end%>                        <% = comment.created_at.strftime ("% Y year% m month% d day% H hour% M minute")%>                    </p>    <% end%>  <% end%>        <!-Form from here-><% if current_user%>  <% = form_with (model: [@tweets, @comment],

 method:: post) do | f |%>    <textarea cols = "30" name = "text" placeholder = "Write a comment" rows = "2"></textarea>    <input type = "submit" value = "SENT">  <% end%><% end%>
edit.html.erb
  <% = form_with model: @mypage, local: true do | f |%>  <% = f.select: prefectures, options_for_select (Mypage.prefectures.keys, selected: @ mypage.prefectures), {prompt: "Please select"},

class: "form-control btn btn-info"%>  <% = f.text_area: text, cols: 30, rows: 10, value: "# {@ mypage.text}"%>  <% = f.submit value: "SENT", class: "game_record"%>        <% end%>

Error details

  • Answer # 1

    In edit.html.erb<% = form_with model: @mypageIt consists of.
    Then the parms [: id] returned from here is the id of mypage, not the id of user.
    def update @mypage = Mypage.find_by (id: params [: id])
    is

  • Answer # 2

    Since the argument has not passed to edit_mypage_path, the id has not been sent.
    If @user is correct
    edit_mypage_path (@user).
    You can't tell if @user is correct without showing the whole thing