I tried to summarize the description that is good for web application development with rails!
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!
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!
** 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
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