[Rails] Procedure for linking databases with Ruby On Rails

Model settings

The sample is `1 to many`

1 side

post.rb


class Post < ApplicationRecord
  has_many :comments
end

Multi-sided

comment.rb


class Post < ApplicationRecord
  belongs_to :post
end

Routing settings

routes.rb


resources :posts do
  resources :comments
end

routes.rb


resources :posts do
  resources :comments, only: [:create, :destroy]
end

How to receive parameters on the form

Enclose in [].

???.html.rb


<%= form_for [@post, @post.comments.build] do |f| %>
<p>
  <%= f.text_field :body %>
</p>
<p>
  <%= f.submit %>
</p>
<% end %>

How to put out and receive at Destroy

From routing, posts can receive parameters with : post_id and comments with `` `: id```.

DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy

???.html.rb


<% if @post.comments.any? %>
<ul>
<% @post.comments.each do |comment|%>
<li><%= comment.body %><span> </span><span><%= link_to "[X]", post_comment_path(@post, comment), method: :delete %></span></li>

???.controller.rb


def destroy
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
  @comment.destroy
  redirect_to post_path(@post)
end

Recommended Posts

[Rails] Procedure for linking databases with Ruby On Rails
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
[Ruby on Rails] View test with RSpec
[Ruby on Rails] About bundler (for beginners)
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Explanation of Ruby on rails for beginners ①
Validation settings for Ruby on Rails login function
Introducing Rspec with Ruby on Rails x Docker
Procedure for operating google spreadsheet with API (ruby)
Publish the app made with ruby on rails
[Ruby on Rails] Select2 introduction memo for Webpacker
[Ruby on Rails] Create a pie chart for each column with Chartkick
Ruby on Rails basics
Ruby On Rails Association
Determine the current page with Ruby on Rails
[Ruby on Rails] Upload multiple images with refile
I made a portfolio with Ruby On Rails
Run Ruby on Rails RSpec tests with GitHub Actions
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Challenge the settings for developing with vue.js on Rails 6
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Created RSS / Atom format sitemap with Ruby on Rails
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
Preparation for developing with Rails
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Use multiple databases with Rails 6.0
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
I tried installing Ruby on Rails related plugin with vim-plug
Definitely useful! Debug code for development in Ruby on Rails
[Ruby on Rails] Add a column with a foreign key constraint
Ruby on Rails for beginners! !! Summary of new posting functions
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Install Ruby on MSYS2 with pacman
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
[Ruby on Rails] Introduced paging function
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
Programming with ruby (on the way)
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function