Conditional branch with helper method

##Overview

This time, I would like to write about an implementation that uses helper methods to conditionally branch when writing a view file.

In particular, I want to display this part when "when the product is not purchased" on the flea market apps. Or I want to hide this display when "the user is not logged in". I will implement a function like that.

Example

ruby:xxx.html.erb


<% if user_signed_in? && current_user.id == @product.user_id %>

This is the part below the description "Only if the user is logged in and that user is the user who listed this item." It is a description to be displayed.

ruby:xxx.html.erb


<% if @item.item_purchase.present? %>

This is the part below the description "Only if data exists in the item_purchase column of @item" It is a description to be displayed.

Not only written here, I think that there are various implementations, but I will output it as the work content.

Recommended Posts

Conditional branch with helper method
Java conditional branch
java conditional branch
[Java] Conditional branch
Integer check method with ruby
Understand the helper method form_with
Various method tests with MockRestServiceServer
Return partial template (partial) with helper
[ruby] Method call with argument
[Ruby] conditional branch if statement
Simulate the simplex method with GUI
[Java] Branch enum with switch statement
Consideration of Routing of form_with helper method
[Ruby] Extracting elements with slice method
Introduction to algorithms with java-Shakutori method