[RUBY] Rails Posts and User Linkage


I'm currently developing a memo app. A list of all the memos is displayed, but somehow I want to display a list of only the memos of the logged-in user on the memo list page.

This time we will learn about it.

Linking memos and users

First, add a column called user_id to the notes table (post-type table) to determine who made each post.

class CreateNotes < ActiveRecord::Migration[5.2]
  def change
    create_table :notes do |t|
      t.text :title
      t.integer :user_id
      t.integer :category_id
      t.text :explanation

$ rails db:migrate:reset

Save the id of the user who posted in the memo

When you save the post, put the information in the user_id column you added earlier. About build method


def create
    @note = current_user.notes.build(note_params)
    redirect_to notes_path

Show only user posts on the memo list page.

 <div class='container'>
   <div class='row'>
    <h2>Memo list</h2>
    <table class='table'>

        <% @notes.each do |note| %>
        <% if user_signed_in? && current_user.id == note.user_id %>   #Add here
            <%= link_to note_path(note) do %>
              <%= note.title %>
            <% end %>
          <td><%= note.category.name %></td>
        <% end %>
        <% end %>

Described in each statement (repetition) of the memo list.

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

Is the user logged in with this description? And check if the id of the logged-in user and the id of the user who posted the memo are the same.

If true, it will be displayed! I think this is okay!


I think the explanation is difficult to understand, but I hope it will be helpful. Also, if there are any mistakes, I would appreciate it if you could teach me.

Recommended Posts

Rails Posts and User Linkage
Rails and FormData
Rails valid? And invalid?
line-bot-sdk-java Account linkage between in-house service user and LINE user
[Rails] Implement User search function
[Rails] N + 1 problems and countermeasures
Rails: Difference between resources and resources
Rails6 Couldn't find User with'id' = sign_out and cannot log out
[Rails] require method and permit method
Rails "render method" and "redirect method"
Rails Tutorial Records and Memorandum # 0
rails path and url methods
Rails is difficult and painful!
Introducing Bootstrap and Font-Awesome (Rails)
Rails is difficult and painful! Ⅱ
[Rails] strftime this and that
Rails web server and application server
Implement user registration function and corporate registration function separately in Rails devise
Memorandum [Rails] User authentication Devise
[Rails] Implementation of user logic deletion
[Rails] Save start time and end time
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[Rails] Implementation of user withdrawal function
[Rails] Difference between find and find_by
[Rails] Validation settings and Japanese localization
Rails model and table naming conventions
Rails6 OmniAuth activestorage Get user image
Data linkage with Spark and Cassandra
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
[Rails] Differences and usage of each_with_index and each.with_index
Project ruby and rails version upgrade
Consideration about Rails and Clean Architecture
[rails] Difference between redirect_to and render