
前提
応募ボタンとメッセージボタンを条件分岐で表示させようとしていたのですが、
応募ボタンが出現しません。
ifでtrueならメッセージボタンを、falseなら応募ボタンを表示するようにしていたのですが、
応募ボタンだけが表示されません。
該当のソースコード
plans_controller.rb
1def show 2 if user_signed_in? 3 @plan = Plan.find(params[:id]) 4 @room = Room.all.find_by(plan_id: @plan.id) 5 if @room 6 @alreadyRoom = true 7 else 8 @alreadyRoom = false 9 end 10 11 @currentUserEntry=Entry.where(user_id: current_user.id) 12 @userEntry=Entry.where(user_id: @plan.user_id) 13 unless @plan.user.id == current_user.id 14 @currentUserEntry.each do |cu| 15 @userEntry.each do |u| 16 if cu.room_id == u.room_id then 17 @isRoom = true 18 @roomId = cu.room_id 19 end 20 end 21 end 22 if @isRoom 23 else 24 @room = Room.new 25 @entry = Entry.new 26 end 27 end 28 end 29 end 30 31 def new 32 @plan = Plan.new 33 if @plan.user == current_user 34 redirect_to '/' 35 end 36 end
!インデントの修正10/08/23:01
views/plans/show.html.erb
1 <div class="plan-content-btn"> 2 <% if @isRoom == true %> 3 <%= @isRoom %> 4 <%= @alreadyRoom %> 5 <% if @alreadyRoom == true%> 6 <p class="user-show-room"><a href="/rooms/<%= @roomId %>"class="btn btn-primary btn-lg">メッセージ</a> 7 <% end %> 8 <% else %> 9 <% if @alreadyRoom == false%> 10 <%= form_for @room do |f| %> 11 <%= fields_for @entry do |e| %> 12 <%= e.hidden_field :plan_id, value: @plan.id %> 13 <%= e.hidden_field :user_id, value: @plan.user.id %> 14 <% end %> 15 <%= f.submit "応募", class:"btn btn-primary btn-lg user-show-chat"%> 16 <% end %> 17 <% end %> 18 <% end %> 19 </div>
!追記
1.@isRoom, @alreadyRoom = true : メッセージボタンを表示
2.@isRoom, @alreadyRoom = false : 応募ボタンを表示
になっているかを表示させて試してみたのですが、2の条件が@isRoomのほうがtrueと表示されていました。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/10/08 14:33
2022/10/08 14:49
退会済みユーザー
2022/10/08 16:07
退会済みユーザー
2022/10/09 03:42 編集
2022/10/09 11:23
退会済みユーザー
2022/10/09 14:06
2022/10/09 22:56
退会済みユーザー
2022/10/10 02:02