This and that of conditional branching of rails development

This and that of conditional branching of rails development

I tried to summarize the description that is good for web application development with rails!

I want to be able to edit / delete posts only by the poster!

When creating an application like SNS with rails, everyone can edit the post You can't delete it! The conditional branch below is convenient in such a case!

** It is assumed that devise (Gem) is installed **

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 %> #Postscript
    <%= link_to "To edit", edit_post_path(@post.id) %>
    <%= link_to "delete", post_path, method: :delete %>
  <% end %># Addendum

In this way, the edit / delete part is conditional branching ** If the poster's id matches the logged-in user id ** It's a good idea to enclose it with <% if @ post.user_id == current_user.id%>, which has the meaning of!

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

Isn't this the case, for example? Before logging in, add a new registration / login link in the header. After logging in, follow the My Page and logout links. I want to implement it! The conditional branch below is convenient in such a case!

** It is assumed that devise (Gem) is installed **

** This time, I am using the navigation bar using 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 'Log out', destroy_user_session_path, data: { confirm: "Log outしますか?" }, 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;">sign up</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/users/sign_in" style="color: white;">Login</a>
            </li>
          </ul>
        </div>
      </nav>
  <% end %>

** The <% if user_signed_in?%> Part is important! ** ** This is a conditional branch that says "if the user is logged in"! Combine with <% else%> (otherwise → "if you haven't logged in") for even more power!

I throw a nil error when posting an image

** It is assumed that you have implemented image posting using cloudinary.

This time, I'm using image posting as an example, but what should I do with nil? .. You can use this in that case!

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 makes it a conditional branch that will be displayed if it exists instead of nil!

[Supplement] I made the image size square and bootstrap rounded the image like a Twitter profile image

Finally

So far, 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 your hands and understand!

Recommended Posts

This and that of conditional branching of rails development
[Rails] strftime this and that
Basics of conditional branching and return
This and that of Core Graphics
This and that of exclusive control
This and that of Swift corner Radius
Conditional branching of numbers
Basics of Java development ~ How to write a program (flow and conditional branching) ~
[Rails] Volume that displays favorites and a list of favorites
Calendar implementation and conditional branching in Rails Gem simple calendar
java (conditional branching and repetition)
Digital certificate this and that
Base64 encoder this and that
I tried to verify this and that of Spring @ Transactional
[Rails] Differences and usage of each_with_index and each.with_index
This and that about Base64 (Java)
Comparison of WEB application development with Rails and Java Servlet + JSP
Creating a mixed conditional expression of Rails if statement and unless
Rails Addition of easy and easy login function
This and that of the implementation of date judgment within the period in Java
[Rails] Implementation of validation that maintains uniqueness
Ruby on Rails ~ Basics of MVC and Router ~
Difference between member and collection of rails routes.rb
Rails development environment created with VSCode and devcontainer
A reminder of Docker and development environment construction
[Rails] Ranking and pagination in order of likes
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
Rails and FormData
Introducing the settings and reference articles that are first done in Rails new development
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
Rails Development Transition 2020
Summary of frequently used commands in Rails and Docker
[Rails] Implementation of drag and drop function (with effect)
Rails CRUD function implementation ② (edited and detailed this time)
Docker the development environment of Ruby on Rails project
This and that for editing ini in Java. : inieditor-java
Roughly the flow of web application development with Rails.
iOS app development: Timer app (5. Implementation of alarm and vibration)