2023-08-19 05:29PM
1. 先创建comment_users表
bundle exec rails generate migration create_comment_users
然后增加下面的代码:
class CreateCommentUsers < ActiveRecord::Migration
def change
create_table :comment_users do |t|
t.string :username
t.string :password
t.timestamps
end
end
end
2. 在comments表中增加comment_user_id列, 因为文章和评论用户是多对多关系,中间表是comments
bundle exec rails generate migration add_comment_user_id_to_comments
然后增加下面的代码:
add_reference :comments, :comment_user_id, :integer
3.创建controller
# app/controllers/comment_users_controller.rb
class CommentUsersController < ApplicationController
def new
@comment_user = CommentUser.new
end
def create
@comment_user = CommentUser.new
@comment_user.username = params[:comment_user][:username]
@comment_user.password = params[:comment_user][:password]
@comment_user.save
if @comment_user.username.blank? || @comment_user.password.blank?
redirect_to :back, notice: '用户名和密码不能为空'
return
end
session[:current_comment_user] = @comment_user
redirect_to article_path(params[:article_id])
end
def logout
session[:current_comment_user] = nil
session[:is_comment_user_username] = '尚未登录'
redirect_to article_path(params[:article_id])
end
end
4. 创建model
# app/models/comment_user.rb
class CommentUser < ActiveRecord::Base
has_many :comments
has_many :articles, through: :comments
end
5. 然后在app/models/comment.rb文件中增加:
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :comment_user
end
在app/models/article.rb文件中增加:
class Article < ActiveRecord::Base
has_many :comments
has_many :comment_users, through: :comments
end
6. 增加路由
# config/routes.rb
Rails.application.routes.draw do
resources :comment_users do
collection do
delete :logout
end
end
end
7. 创建app/views/comment_users/new.html.erb
<p>注册评论用户</p>
<%= form_for @comment_user, url: comment_users_path(article_id: params[:article_id]) do |f| %>
<div>
用户名: <%= f.text_field :username %>
<p></p>
</div>
<div>
密码: <%= f.password_field :password %>
<p></p>
</div>
<div>
<%= f.hidden_field :article_id, value: params[:article_id] %>
</div>
<div>
<%= f.submit '注册' %>
</div>
<% end %>
8. 在app/controllers/articles_controller.rb增加下面的代码:
def show
@comments = Comment.where('article_id = ?', params[:id]).all
end
9. 在 app/views/articles/show.html.erb增加下面的代码:
<div class='comment'>
<% if session[:current_comment_user].present? %>
欢迎您: <%= session[:current_comment_user]['username']%>
<%= link_to '退出', logout_comment_users_path(article_id: params[:id]), method: :delete %></br>
<% end %>
<% if session[:current_comment_user].present? %>
<%= form_tag "/comments", method: 'post' do %>
<input type='hidden' name='comment_user_id' value='<%= params[:comment_user_id] %>' />
<input type='hidden' name='article_id' value='<%= params[:id] %>' />
<p>正文:
<%= text_field_tag "content" %>
<%= submit_tag "提交", style:' margin-left: 5px;' %>
</p>
<% end %>
<% else %>
<p>
<%= link_to '发表评论', new_comment_user_path(article_id: params[:id]) %>
</p>
<p>请先点击发表评论,登录后再进行评论。</p>
<% end %>
<p style='font-weight: bold;'>评论列表: </p>
<% @comments.each do |comment| %>
<p>姓名: <%= comment.comment_user.try(:username)%></p>
<p>评论正文: <%= comment.content %></p>
<% end %>
</div>
</div>
增加完上面几步,就可以实现只有登录的评论用户才可以进行评论了
登录
请登录后再发表评论。
评论列表:
目前还没有人发表评论