Creating a mixed conditional expression of Rails if statement and unless

We made adjustments for the final task. At that time, the conditional expression has a special pattern of using both if and unless. I tried to challenge. I will post it as a memorandum.

This time it is described in haml.

sample.rb


#In the view file

.item-show-page__link-btn
        -if @item.buyers.present?
          %p SOLD OUT
        -else
          - unless user_signed_in? && @item.user_id == current_user.id
            = link_to purchase_item_path(@item.id), class: "item-show-page__link" do
To purchase screen
          - else
            %p Seller cannot purchase

The contents of the method @item Contains listing information. buyers This is the method of purchase history.

Commentary

if〜else If the purchase history ID exists, SOLD OUT is displayed.

unless~else Nested inside the else of the if statementdoing. From there, the unless conditional expression is entered. If the item you listed does not match your user ID, a link to the purchase screen will appear. If not, it will appear as "Seller cannot purchase".

That is all.

Recommended Posts

Creating a mixed conditional expression of Rails if statement and unless
[Ruby] if statement concept of conditional expression
Rails Basics of creating a new application
Basics of java basics ② ~ if statement and switch statement ~
[Rails] Volume that displays favorites and a list of favorites
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
About for statement and if statement
Creating a batch of Liferay
[Rails] Creating a search box
[Ruby] Conditional bifurcation unless statement
[Ruby] conditional branch if statement
(Ruby on Rails6) Creating a database and displaying it in a view
Basics of conditional branching and return
[Rails] Creating a new project with rails new
Java study # 4 (conditional branching / if statement)
[Rails] Differences and usage of each_with_index and each.with_index
About if statement and branch processing
[Java] Conditional branching is an if statement, but there is also a conditional operator.
rails c does not start and a lot of line errors appear
Let's get rid of Devise's sickness and lead a comfortable Rails life!