前提
本のタイトルと感想を投稿できるアプリケーションを作っています。
ユーザー一覧で
◆ユーザー画像・名前・自己紹介文(introduction)・Show(投稿者の詳細ページへ飛ぶリンク)
が表示されています。
※ネットで調べてみたのですが、調べ方が悪いらしく知りたい情報にたどり着けませんでした...
実現したいこと(動作)
Showリンクを踏んだ際にユーザー情報(image,name,introduction)を編集する機能に関して、投稿者と投稿者以外のユーザーで遷移先を以下のように実装させたいです。
1.ユーザーに関する編集機能で、ユーザー一覧からリンク(Showリンク)を踏むと投稿したユーザーの詳細ページへ飛ぶ
2.投稿者なら編集リンクを踏むと投稿者自身の情報を編集が可能
3.投稿者以外ならば一旦自分(user)の詳細画面に飛び、情報を編集可能
該当のソースコード
ruby(users/show.html.erb)
1<main> 2 <h1>User info</h1> 3 <%= image_tag @user.get_profile_image(100,100) %><br> 4 <h3>name <%= @user.name %></h3> 5 <h3>introduction <%= @user.user_introduction %> </h3> 6 <% if @user.id == current_user.id? %> 7 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 8 <% else %> 9 <p><%= link_to "プロフィール編集", user_path(user.id) %></p> 10 <% end %> 11</main>
試したこと
2つ試してみたのですが...
Ruby
1 ①<h3>introduction <%= @user.user_introduction %> </h3> 2 #リンク自体は表示させたいのでこれだと表示されないので別のものを使う必要がある? 3 <% if @user.id == current_user.id? %> 4 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 5 <% else %> 6 7 ②<h3>introduction <%= @user.user_introduction %> </h3> 8 #ログインユーザーなら誰でも編集できてしまうが、この辺りを変えれば実装したいものができる? 9 <% if user_signed_in? %> 10 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 11 <% else %> 12
補足情報(FW/ツールのバージョンなど)
Ruby on Rails3.1.2
ruby(route)
1Rails.application.routes.draw do 2 get 'homes/about', to: 'homes#show', as: :about 3 post 'books/book.id' => 'books#create' 4 devise_for :users 5 root to: "homes#top" 6 resources :books, only: [:show, :edit, :index, :create, :destroy] 7 resources :users, only: [:show, :edit, :index, :update] 8 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 9end
ruby(users_controller)
1class UsersController < ApplicationController 2 def show 3 @user = User.find(params[:id]) 4 @books = @user.books 5 end 6 7 def edit 8 @user = User.find(params[:id]) 9 end 10 11 def index 12 @users = User.all 13 end 14 15 def update 16 @user = User.find(params[:id]) 17 @user.update(user_params) 18 redirect_to user_path 19 end 20 21 private 22 23 def user_params 24 params.require(:user).permit(:name, :profile_image, :user_introduction) 25 end 26 27end 28
ruby(users_table)
1create_table "users", force: :cascade do |t| 2 t.string "email", default: "", null: false 3 t.string "encrypted_password", default: "", null: false 4 t.string "reset_password_token" 5 t.datetime "reset_password_sent_at" 6 t.datetime "remember_created_at" 7 t.string "name" 8 t.datetime "created_at", precision: 6, null: false 9 t.datetime "updated_at", precision: 6, null: false 10 t.text "user_introduction" 11 t.index ["email"], name: "index_users_on_email", unique: true 12 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 13 end
ruby(users/index.html.erb)
1 <main> 2 <h2>Users</h2> 3 <% @users.each do |user| %> 4 <p>image</p><br><%= image_tag user.get_profile_image(100,100) %> 5 <p>name <br><%= user.name %></p><br><%= link_to "Show", user_path(user.id) %> 6 <% end %> 7 </main>
ruby(users/edit.html.erb)
1<h1>User info</h1> 2<%= form_with model: @user do |f| %> 3 <label for="inputName">Name</label><br><%= f.text_field :name, autfocus: true, id:"inputName" %><br> 4 <label for="inputImage">Image</label><br><%= f.file_field :profile_image, placeholder:"プロフィール画像", accept:"image/*" %><br> 5 <label for="inputIntroduction">Introduction</label><br><%= f.text_area :user_introduction, autfocus: true, id:"inputIntroduction" %><br> 6 <%= f.submit "Update User" %> 7<% end %>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/05 09:15
2022/08/07 08:50 編集