FRAMEWORK » RUBY_ON_RAILS » TUTORIAL
Model association
Creating comments
for articles
.
shell
rails generate model Comment commenter:string body:text article:references
rails db:migrate
rails generate controller Comments
Tell rails that articles has many comments
ruby
## app/models/article.rb
class Article < ApplicationRecord
has_many :comments
validates :title, presence: true, length: { minimum: 5 }
end
## config/routes.rb
resources :articles do
resources :comments
end
Adding and viewing comments
ruby
## app/controllers/comments_controller.rb
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
erb
<!-- app/views/articles/show.html.erb -->
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Comments</h2>
<% @article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_with(model: [ @article, @article.comments.build ], local: true) do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>