[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise

Introduction

I wanted to make an article posting app with Rails, and when I introduced Scaffold to cut the procedure, I stumbled on connecting posts and users, so I shared my knowledge If you want to know more, please see this document.

Preparation

Post function generation with Scaffold

First, generate a posting function with Scaffold. Insanely easy. db: migrate Don't forget

$ rails g scaffold Post content:string
$ db:migrate

devise Refer to this article. Very easy to understand

Linking

belongs_to and has_many

app/models/user.rb


class User < ApplicationRecord

  #Add the following.Don't forget the plural s
  has_many :posts,  dependent: :destroy
end

app/models/post.rb


class Tip < ApplicationRecord
  #Add the following.To be on the safe side, make sure empty posts are rejected
  belongs_to :user
  validates :content, presence: true
end

Add user_id column

$ rails g migration add_user_id_to_posts user_id:integer
$ rails db:migrate

*** This completes the link between the User model and the Post model in terms of mechanism ***

Added to controller

app/controllers/posts_controller.rb


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def post_params
      params.require(:post).permit(:content)
    end
end

Modify the above code automatically generated by Scaffold as follows.

app/controllers/posts_controller.rb


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def post_params
      params.require(:post).permit(:content).merge(user_id: current_user.id)
    end
end

This completes. Let's run it, go to localhost: 3000 / posts and post it! ‼

Recommended Posts

[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
[Rails] How to get the user information currently logged in with devise
[Rails] How to log in with a name by adding a devise name column
How to make the schema of the URL generated by Rails URL helper https
[Rails] How to decide the destination by "rails routes"
[Rails] How to search by multiple values ​​with LIKE
[Rails] How to install devise
How to connect the strings in the List separated by commas
[With back tricks] How to introduce React to the simplest Rails
[Rails] How to delete images uploaded by carrierwave (using devise)
[Rails] How to display the list of posts by category
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
Connect to Rails server with iPhone
How to get along with Rails
How to add the delete function
[Rails] How to introduce kaminari with Slim and change the design
I tried to implement the image preview function with Rails / jQuery
[Rails] How to put a crown mark on the ranking function
How to use MinIO with the same function as S3 Use docker-compose
[Rails 5] How to display the password change screen when using devise
How to implement search function with rails (multiple columns are also supported)
How to decorate the radio button of rails6 form_with (helper) with CSS
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Java] How to use the hasNext function
[Rails] How to use rails console with docker
[Rails] How to use the map method
How to access Socket directly with the TCP function of Spring Integration
[Swift] How to implement the countdown function
How to update user edits in Rails Devise without entering a password
[Rails] Use devise to get information about the currently logged in user
How to implement TextInputLayout with validation function
How to compare only the time with Rails (from what time to what time, something like)
How to handle sign-in errors with devise
[Note] How to use Rails 6 Devise + cancancan
[Processing × Java] How to use the function
How to display the text entered in text_area in Rails with line breaks
How to implement image posting using rails
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to translate devise into Japanese
How to implement the email authentication function at the time of user registration
How to build Rails 6 environment with Docker
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
[Rails] How to read the XML file uploaded from the screen with Hash type
How to get the ID of a user authenticated with Firebase in Swift
[Rails] How to register multiple records in the intermediate table with many-to-many association
How to implement image posting function using Active Storage in Ruby on Rails
[Rails] How to operate the helper method used in the main application with Administrate
[Rails] Implementation procedure when public / private functions are added to the posting function
The road to Japaneseizing Rails devise error messages
Use [Rails] devise Guest user function (for portfolio)
How to use rails g scaffold, functions, precautions
[Swift] How to implement the LINE login function
Posting function implemented by asynchronous communication in Rails
How to output CSV created by Rails to S3
[swift5] How to implement the Twitter share function
How to implement the breadcrumb function using gretel
[Swift] How to link the app with Firebase
[For beginners] How to implement the delete function
The code I used to connect Rails 3 to PostgreSQL 10
How to separate .scss by controller in Rails