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
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?&¤t_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
-
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
Related articles
- ruby - when i try to update rails, the empty value is saved
- ruby on rails 5 - i want to update the received parameters with nil
- ruby on rails 6 - [rails] i want to update the display of chat room participants etc in real time
- ruby - rails db: migrate is not possible
- ruby - data is not updated by update action rails
- ruby on rails - i can't update rubygems
- ruby on rails 5 - can i update with just the update action?
- ruby - rails db: migrate is not possible
- ruby - when i try to update rails, i get a routing error
- ruby on rails - i want to avoid displaying duplicate data for each shop
- ruby on rails - i want to change from the state where only one posted image is displayed to the state where two posted images ar
- ruby on rails uninitialized constant error
- ruby on rails - i want to deploy to aws ec2 server
- ruby - the last db setting doesn't work when deploying rails app on aws
- ruby - i want to display specific columns in rails in descending order of numbers
- [ruby on rails] how to write when getting a model nested in multiple stages with includes
- ruby on rails - when i run rails s, i get
- ruby on rails - i installed ruby with rbenv, but the version does not change
- ruby on rails 5 - scope by specifying conditions for parent-child relationship
Related questions
- javascript - i want the scroll bar to always appear at the bottom of the chat screen
- ruby - [rspec] error in "dependent: destoy" test: how to resolve "validation failed"
- ruby - [rails] when rails s is executed, an error "cannot load database configuration" appears and it is forcibly term
- ruby - [rails] i can't install the add favorite button
- ruby - when i run rails s, i get a message like error loading the'sqlite3' and the server doesn't start properly
- ruby - an error occurred when implementing the micropost search function using ransack
- ruby - how to upload images directly from rails to s3 on aws
- ruby - about accelerating heroku's rails app
- ruby on rails - regarding the priority of rails order method
In edit.html.erb
<% = form_with model: @mypage
It 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