[Ruby] About conditional branching of rails development

2 minute read

rails Development of conditional branch

I have summarized a good description that it is web application development with rails!

I want to be able to edit/delete only the post for the post!

When making an application like SNS with rails, every user can edit posts You can’t delete it! In that case, the conditional branch below is useful!

Assuming that you have installed devise(Gem)

post.rb


  belongs_to :user

user.rb


  has_many :posts

route.rb


  resources :post, only: [:show]

post_controller.rb


  def show
      @film = Post.find(params[:id])
  end

show.html.erb


  <% if @post.user_id == current_user.id %> # Add
    <%= link_to "Edit", edit_post_path(@post.id) %>
    <%= link_to "delete", post_path, method: :delete %>
  <% end %> # add

In this way, edit/delete parts are conditional If the contributor id matches the logged-in user id It is good to enclose it with <% if @post.user_id == current_user.id %> which has the meaning of!

I want to change the description depending on who is logged in or not!

Is there something like this? Before logging in, add a new registration/login link in the header After logging in, click the link for My Page or Logout I want to implement it! The conditional branch below is useful in this situation!

Assuming that you have installed devise(Gem)

This time, I am using a navigation bar that uses bootstrap Click here for bootstrap

layout/application.html.erb


  <% if user_signed_in? %>
      <nav class="navbar fixed-top navbar-expand-lg navbar-light">
        <a class="navbar-brand" href="/">Home</a>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item">
              <a class="nav-link" href="/post/new" style="color: white;"> Post <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/users/<%= current_user.id %>">My page</a>
            </li>
            <li class="nav-item">
              <%= link_to'logout', destroy_user_session_path, data: {confirm: "Are you sure you want to logout?" }, method: :delete, class:"nav-link"%>
            </li>
          </ul>
        </div>
      </nav>
  <% else %>
      <nav class="navbar fixed-top navbar-expand-lg navbar-light">
        <a class="navbar-brand" href="/home">Top</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation ">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item">
              <a class="nav-link" href="/users/sign_up" style="color: white;">New registration</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/users/sign_in" style="color: white;">Login</a>
            </li>
          </ul>
        </div>
      </nav>
  <% end %>

**<% if user_signed_in? %> part is important! ** This is a conditional branch that says “if the user is logged in!” In combination with <% else %> (otherwise → “If you haven’t logged in”), you’ll be even more powerful!

I spit a nil error when posting an image

**Assuming you have implemented image posting using cloudinary

This time, I’m taking an example of image posting, but what about other nil? . You can use this if you want!

db/migrate/OOOOOOOOOOOOOO_add_image_to_posts.rb


  def change
    add_column :posts, :image, :string
  end

post_controller.rb


  def show
      @film = Post.find(params[:id])
  end

show.html.erb


<% if @post.image.present? %>
      <%= image_tag @post.image_url, :size =>'150x150', class: "img_fluid rounded-circle" %>
   <% end %>

**The <% record.column.present? %> part is important! ** This will result in a conditional branch being displayed instead of nil if it exists!

[Supplement] The image size is square and the image is rounded by bootstrap like Twitter profile image.

Finally

Up to this point, I’ve described three personally used conditional branches! Please point out any mistakes!

Conditional branch (if) statements are common and important in programming! Let’s actually move our hands to understand!