前提・実現したいこと
表示させるヘッダーを条件分岐によって変えたい。
または1つのヘッダー内容を条件分岐によって変えたい。
発生している問題・エラーメッセージ
ActionView::MissingTemplate in Profiles#index
Missing partial shered/_header2 with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
- "/projects/kiite-proto1/app/views"
- "/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.8.1/app/views"
- "/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actiontext-6.0.4.8/app/views"
- "/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionmailbox-6.0.4.8/app/views"
Extracted source (around line #4):
<%= render "shared/header" %>
<% else %>
<%= render "shered/header2" %>
<% end %>
ruby
1<% if user_signed_in? %> 2 <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete, class: "logout" %></li> 3 <li><%= link_to 'プロフィール登録', new_profile_path, class: "registration-profile" %></li> 4 <li><%= link_to 'マイプロフィール', profile_path(current_user.id), class: "my-profile" %></li> 5 <% else %> 6 <li><%= link_to 'ログイン', new_user_session_path, class: "login" %></li> 7 <li><%= link_to '新規登録', new_user_registration_path, class: "sign-up" %></li> 8 <% end %>
ログイン状態かつプロフィール情報があればマイプロフィールへのリンクを表示
ログイン状態かつプロフィール情報がなければプロフィール登録へのリンクを表示
という条件分岐をしたいのですが、良い表記方法がわかりません。
色々試しましたが、IDの取得ができなかったり、ノーメソッドエラーが起きてしまいます。
該当のソースコード
routes.rb
ruby
1Rails.application.routes.draw do 2 devise_for :users 3 root to: "profiles#index" 4 resources :profiles do 5 resources :favorites, only: [:create, :destroy] 6 resources :ratings, only: [:new, :create, :edit, :update] 7 end 8 resources :users, only: [:edit, :update] do 9 get :favorites, on: :collection 10 end 11 resources :messages, only: [:create] 12 resources :rooms, only: [:create, :show] 13 resources :tags do 14 get "profiles", to: "profiles#search" 15 end 16end
user.rb
ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_one :profile 8 has_many :favorites, dependent: :destroy 9 has_many :ratings, dependent: :destroy 10 11end
profile.rb
ruby
1class Profile < ApplicationRecord 2 mount_uploader :image, ImageUploader 3 4 belongs_to :user 5 has_many :room_users, dependent: :destroy 6 has_many :rooms, through: :room_users 7 has_many :messages, dependent: :destroy 8 has_many :profile_tags, dependent: :destroy 9 has_many :tags, through: :profile_tags 10 has_many :favorites, dependent: :destroy 11 has_many :ratings, dependent: :destroy 12 13 validates :nickname, presence: true 14 15 def save_tag(sent_tags) 16 current_tags = self.tags.pluck(:tag_name) unless self.tags.nil? 17 old_tags = current_tags - sent_tags 18 new_tags = sent_tags - current_tags 19 20 old_tags.each do |old| 21 self.tags.delete Tag.find_by(tag_name: old) 22 end 23 24 new_tags.each do |new| 25 new_profile_tag = Tag.find_or_create_by(tag_name: new) 26 self.tags << new_profile_tag 27 end 28 end 29end
profile/index.html.erb
ruby
1<% if @profiles.find_by(user_id: current_user.id).nil? %> 2 <%= render "shared/header" %> 3<% else %> 4 <%= render "shered/header2" %> 5<% end %>
__header.html.erb
ruby
1<div class='nav'> 2 <ul class='lists-right'> 3 <% if user_signed_in? %> 4 <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete, class: "logout" %></li> 5 <li><%= link_to 'プロフィール登録', new_profile_path, class: "registration-profile" %></li> 6 <% else %> 7 <li><%= link_to 'ログイン', new_user_session_path, class: "login" %></li> 8 <li><%= link_to '新規登録', new_user_registration_path, class: "sign-up" %></li> 9 <% end %> 10 </ul> 11 </div>
_header2.html.erb
ruby
1<div class='nav'> 2 <ul class='lists-right'> 3 <% if user_signed_in? %> 4 <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete, class: "logout" %></li> 5 <li><%= link_to 'マイプロフィール', profile_path(current_user.id), class: "my-profile" %></li> 6 <% else %> 7 <li><%= link_to 'ログイン', new_user_session_path, class: "login" %></li> 8 <li><%= link_to '新規登録', new_user_registration_path, class: "sign-up" %></li> 9 <% end %> 10 </ul> 11 </div>
自分で調べたことや試したこと
ヘッダー内で条件分岐をするパターンと、ヘッダーを2つ用意して条件分岐によって表示するヘッダーを変えるパターンを試しましたが上手くいきません。どなたか良い表記方法をご教授いただければ幸いです。
回答1件
あなたの回答
tips
プレビュー