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

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

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

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

Ruby on Rails

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

Q&A

解決済

1回答

1033閲覧

条件分岐で該当者以外の不正アクセスを防ぎたいです。

guch

総合スコア1

Ruby

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

Ruby on Rails

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

0グッド

0クリップ

投稿2021/07/17 10:07

前提・解決したいこと

Webアプリ内容
Rubyで結婚式用のフリーランスの人を探すアプリを製作中です。
Bootstrapを使用しております。

名称
user = フリーランスのスタッフを探しているカップル
staff_member = フリーランスのプランナーやヘアメイクなど。
contact = user がstaff_memberへ問い合わせする
※user、staff_memberはdeviceを使用。

詳細
userからstaff_memberへ問い合わせ機能(contact)を作成し、
staff_memberのマイページで問い合わせ内容を表示。
問い合わせしたuserの名前をクリックするとuserのユーザーページにアクセスできます。

解決したいこと
ユーザーページにアクセスできる人を制限したいです。

アクセス可
・user本人
・問い合わせされたstaff_member

アクセス不可
・それ以外の方

発生している問題・エラーメッセージ

イメージ説明

error

1NoMethodError in UsersController#show 2 3undefined method `staff_member_id' for nil:NilClass

該当のソースコード

Ruby

1routes 2 3Rails.application.routes.draw do 4 5 devise_for :users 6 devise_for :staff_members 7 8 root to: "staff_members#index" 9 get '/mypage', to: 'staff_members#mypage' 10 get '/userpage', to: 'users#userpage' 11 12 resources :staff_members do 13 resources :contacts 14 end 15 resources :users 16end 17

Ruby

1StaffMembersController 2 3class StaffMembersController < ApplicationController 4 before_action :authenticate_staff_member!, only: [:mypage] 5 before_action :set_staff_member, only: :show 6 before_action :configure_permitted_parameters, if: :devise_controller? 7 8 def index 9 @staff_members = StaffMember.order('created_at DESC') 10 @user = User.all 11 end 12 13 def new 14 @staff_member = StaffMember.new 15 end 16 17 def show 18 @contact = Contact.new 19 @contacts = @staff_member.contacts.includes(:user) 20 end 21 22 def mypage 23 redirect_to staff_member_path(current_staff_member) 24 end 25 26 def edit 27 end 28 29 def create 30 @staff_member = StaffMember.create(staff_member_params) 31 end 32 33 private 34 35 def staff_member_params 36 params.require(:staff_member).permit(:image, :last_name, :first_name, :area, :since, :text) 37 .merge(staff_member_id: current_staff_member.id, contact_id: contact.id ) 38 end 39 40 def set_staff_member 41 @staff_member = StaffMember.find(params[:id]) 42 end 43end

Ruby

1ContactsController 2 3class ContactsController < ApplicationController 4 5 def create 6 @contact = Contact.create(contact_params) 7 redirect_to root_path root_url 8 end 9 10 def show 11 @contact = Contact.find(params[:staff_member_id]) 12 end 13 14 private 15 16 def contact_params 17 params.require(:contact).permit(:text).merge(user_id: current_user.id, staff_member_id: params[:staff_member_id]) 18 end 19end

Ruby

1UsersController 2 3class UsersController < ApplicationController 4 before_action :authenticate_user!, only: [:userpage] 5 before_action :move_to_index, only: :show 6 7 def show 8 @user = User.find(params[:id]) 9 @contact = Contact.find(params[:id]) 10 end 11 12 def userpage 13 redirect_to user_path(current_user) 14 end 15 16 private 17 18 def move_to_index 19 redirect_to root_path 20 unless user_signed_in? || staff_member_signed_in? && @contact.staff_member_id == current_staff_member.id 21 end 22 end 23end

HTML

1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="utf-8" /> 5 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> 6 <meta name="description" content="" /> 7 <meta name="author" content="" /> 8 <title>Modern Business - Start Bootstrap Template</title> 9 <!-- Favicon--> 10 <link rel="icon" type="image/x-icon" href="assets/favicon.ico" /> 11 <!-- Bootstrap icons--> 12 <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css" rel="stylesheet" /> 13 <!-- Core theme CSS (includes Bootstrap)--> 14 <link href="css/styles.css" rel="stylesheet" /> 15 </head> 16 <body class="d-flex flex-column"> 17 <main class="flex-shrink-0"> 18 <%= render partial: "layouts/shered/navigation" %> 19 <!-- Staffmember.image --> 20 <section class="py-5"> 21 <div class="container px-5"> 22 <div class="col-lg-6 col-xl-7"> 23 <%= image_tag "user_mypage.jpg",resize: '500x500', class:"bg-featured-blog" %> 24 </div> 25 </div> 26 </section> 27 <section class="py-5 bg-light"> 28 <div class="container px-5"> 29 <div class="row gx-5"> 30 <%# ここからNewsの部分 %> 31 <div class="col-xl-8"> 32 <h2 class="fw-bolder fs-5 mb-4">Couple Profile</h2> 33 <!-- News item--> 34 <div class="mb-5"> 35 <div class="small text-muted">area</div> 36 <h5><%= @user.area%></h5></a> 37 </div> 38 <div class="mb-4"> 39 <div class="small text-muted">name</div> 40 <h5><%= @user.first_name1 %> 41 <%= @user.last_name %> 42 </h5></a> 43 </div> 44 <div class="mb-4"> 45 <div class="small text-muted">name</div> 46 <h5><%= @user.first_name2 %> 47 <%= @user.last_name %> 48 </h5> 49 </div> 50 <!-- News item--> 51 <div class="mb-5"> 52 <div class="small text-muted">anniversary</div> 53 <h5><%= @user.anniversary %> 54 </h5></a> 55 </div> 56 </div> 57 </div> 58 <div class="mb-5"> 59 <div class="small text-muted">email</div> 60 <h5><%= @user.email %></h5></a> 61 </div> 62 </div> 63 </section> 64 <!-- Footer--> 65 <%= render partial: "layouts/shered/footer" %> 66 <!-- Bootstrap core JS--> 67 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js"></script> 68 <!-- Core theme JS--> 69 <script src="js/scripts.js"></script> 70 </body> 71</html>

試したこと

UsersControllerのshowに@contact = Contact.find(params[:id])を追加。

バージョン

Ruby 2.6.5

その他

初めての質問です。
足りない情報やその他何かございましたら、ご指摘いただけますと幸いです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

「@contact = Contact.find(params[:id])を追加。」を move_to_index にて行いましょう

投稿2021/07/17 10:17

winterboum

総合スコア23401

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

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

guch

2021/07/17 11:14

winterboum様 ご回答いただきありがとうございます。 「@contact = Contact.find(params[:id])を追加。」を move_to_indexにて行った結果、 ActiveRecord::RecordNotFound in UsersController#show Couldn't find Contact with 'id'=3 こちらのエラーが発生してしまいました。 また別件のエラーということですよね・・・
winterboum

2021/07/17 12:39

ああ、多分そのidはuserのidだね。 contaact のidは送ってないの? どのviewのどのボタンから来るのかな、そっちにも問題ありそう
guch

2021/07/18 00:11

winterboum様 ご返信いただきありがとうございます。 <%= link_to contact.user.last_name, "/users/#{contact.user_id}" %> staff_members/show.html.erbからuser/show.html.erbに飛ぶようにしております
winterboum

2021/07/18 03:02

それだとcontactの情報が消えてしまいますね。 user_path(contact.user, contact_id: contact.id) にして 送りましょう。 そうすると @contact = Contact.find(params[:contact_id])
guch

2021/07/18 13:26

winterboum様 ありがとうございます。 おかげさまで解決いたしました。 link_toで送っていた情報が異なっていたとは、かなり勉強になりました。 復習して、理解度を深めていきたいと思います。 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問