帖子评分 - Rails
Posts Rating - Rails
我正在 rails 实施评级系统 post 秒。
在查看 post 时,可以通过单击单选按钮对 post 进行评分。
下面是代码。考虑 仅 post 和评分 不考虑标签、主题..
并且在我的概念中没有用户可以在他需要时进行评分,应该添加 post 的现有评分。
但是,当我在日志中执行此操作时,它显示以下内容:
服务器日志:
Started PATCH "/posts/34" for 127.0.0.1 at 2015-12-08 18:36:55 +0530
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"l3aae99V424OyKVt5ULmqX2Mcs7DY2GYBskbLyhqqNENDn24ldCDAt4gNcgjESlFR6eaP0vcvrcoOerGE9lH5A==", "post"=>{"rating_ids"=>["5"]}, "commit"=>"Rate", "id"=>"34"}
Post Load (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 34]]
CACHE (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "34"]]
(0.0ms) begin transaction
SQL (4.0ms) INSERT INTO "ratings" ("star", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["star", 5], ["post_id", 34], ["created_at", "2015-12-08 13:06:55.626133"], ["updated_at", "2015-12-08 13:06:55.626133"]]
(216.0ms) commit transaction
(0.0ms) begin transaction
Rating Load (1.0ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."id" = ? LIMIT 1 [["id", 5]]
Rating Load (0.0ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."post_id" = ? [["post_id", 34]]
SQL (2.0ms) UPDATE "ratings" SET "post_id" = NULL WHERE "ratings"."post_id" = ? AND "ratings"."id" IN (4, 25) [["post_id", 34]]
SQL (3.0ms) UPDATE "ratings" SET "post_id" = ?, "updated_at" = ? WHERE "ratings"."id" = ? [["post_id", 34], ["updated_at", "2015-12-08 13:06:55.878147"], ["id", 5]]
(170.0ms) commit transaction
Redirected to http://localhost:3000/topics/9
Completed 302 Found in 489ms (ActiveRecord: 397.0ms)
将 post.id
更改为 NULL
SQL (2.0ms) UPDATE "ratings" SET "post_id" = NULL WHERE "ratings"."post_id" = ? AND "ratings"."id" IN (4, 25) [["post_id", 34]]
我不知道这是怎么发生的以及如何克服这个所以,请帮忙。
它改变评级数据库如下:
第 1 列:id,第 2 列:star,第 3 列:post_id
1,1,NULL
2,2,NULL
3,3,NULL
4,4,NULL
5,5,34
6,4,NULL
7,1,NULL
8,1,NULL
9,5,NULL
10,1,NULL
11,5,NULL
12,1,NULL
13,4,NULL
14,3,NULL
15,4,NULL
16,4,NULL
17,4,NULL
18,2,NULL
19,1,NULL
20,5,NULL
21,3,NULL
Post 型号:
class Post < ActiveRecord::Base
belongs_to :topic
has_many :comments
has_and_belongs_to_many :tags
has_many :ratings
end
评级模型:
class Rating < ActiveRecord::Base
belongs_to :post
end
Post show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @posts.name %> (
<%= @posts.topic.name %> )
</p>
<p>
<strong>Email:</strong>
<%= @posts.email %>
</p>
<p>
<strong>Message:</strong>
<%= @posts.message %>
</p>
<strong>Tags:</strong>
<% @posts.tags.each do |tag| %>
<div>
<%= tag.name %> <br>
</div>
<% end %>
<br>
<strong>Rating:</strong>
<%= @posts.ratings.group(:star).count %>
<%= form_for @posts do |f| %>
<% (1..5).each do |rating| %>
<%= radio_button_tag "post[rating_ids][]", rating %>
<%= rating %>
<% end %>
<%= f.submit('Rate') %>
<% end %>
<%= link_to 'Comments', post_comments_path(@posts) %>
<%= link_to 'Edit', edit_post_path(@posts) %> |
<%= link_to 'Back', topic_posts_url(@posts.topic) %>
Post 控制器:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
if params[:topic_id].to_i > 0
@topic = Topic.find(params[:topic_id])
@posts = @topic.posts.paginate(page: params[:page], per_page: 10)
else
@posts = Post.eager_load(:topic).paginate(page: params[:page], per_page: 10)
end
end
# GET /posts/1
# GET /posts/1.json
def show
@posts = Post.find(params[:id])
@tags = @posts.tags
@comment = Comment.new(:post => @posts)
end
# GET /posts/new
def new
@topic = Topic.find(params[:topic_id])
@posts = @topic.posts.new
end
# GET /posts/1/edit
def edit
@posts = Post.find(params[:id])
@tags = @posts.tags
end
# POST /posts
# POST /posts.json
def create
@topic = Topic.find(params[:topic_id])
@posts = @topic.posts.build(post_params)
respond_to do |format|
if @posts.save
format.html { redirect_to topic_url(@posts.topic_id), notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @posts }
else
format.html { render :new }
format.json { render json: @posts.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
@posts = Post.find(params[:id])
@tags = @posts.tags
respond_to do |format|
@posts.ratings.create(:star => params[:post][:rating_ids][0].to_i)
if @posts.update(post_params)
format.html { redirect_to topic_url(@posts.topic_id), notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @posts }
else
format.html { render :edit }
format.json { render json: @posts.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@posts.destroy
respond_to do |format|
format.html { redirect_to topic_url(@posts.topic_id), notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@posts = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:name, :email, :message, :topic_id, {tag_ids:[]}, rating_ids:[])
end
end
我是 rails 的新手,我需要在不使用任何 gem 的情况下实现它。
请帮助它如何将 post_id 更改为我在服务器日志中表示的 NULL ..
由于您在更新 post 之前保存了评分,因此将 post_params
更改为:
def post_params
params.require(:post).permit(:name, :email, :message, :topic_id, {tag_ids:[]})
end
因为您直接从参数中保存评分。
但是您应该考虑更改您的结构以使用嵌套属性之类的东西,它会自动 save/update 评级。
这是因为在您手动创建 Rating
之后会出现 @posts.update(rating_ids:[5])
,这表示此 post 的评分应该只有 id 5,而不是 5 星
此外,您确定要让未经身份验证的用户访问 post 编辑吗?
我正在 rails 实施评级系统 post 秒。
在查看 post 时,可以通过单击单选按钮对 post 进行评分。 下面是代码。考虑 仅 post 和评分 不考虑标签、主题..
并且在我的概念中没有用户可以在他需要时进行评分,应该添加 post 的现有评分。
但是,当我在日志中执行此操作时,它显示以下内容:
服务器日志:
Started PATCH "/posts/34" for 127.0.0.1 at 2015-12-08 18:36:55 +0530
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"l3aae99V424OyKVt5ULmqX2Mcs7DY2GYBskbLyhqqNENDn24ldCDAt4gNcgjESlFR6eaP0vcvrcoOerGE9lH5A==", "post"=>{"rating_ids"=>["5"]}, "commit"=>"Rate", "id"=>"34"}
Post Load (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 34]]
CACHE (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "34"]]
(0.0ms) begin transaction
SQL (4.0ms) INSERT INTO "ratings" ("star", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["star", 5], ["post_id", 34], ["created_at", "2015-12-08 13:06:55.626133"], ["updated_at", "2015-12-08 13:06:55.626133"]]
(216.0ms) commit transaction
(0.0ms) begin transaction
Rating Load (1.0ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."id" = ? LIMIT 1 [["id", 5]]
Rating Load (0.0ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."post_id" = ? [["post_id", 34]]
SQL (2.0ms) UPDATE "ratings" SET "post_id" = NULL WHERE "ratings"."post_id" = ? AND "ratings"."id" IN (4, 25) [["post_id", 34]]
SQL (3.0ms) UPDATE "ratings" SET "post_id" = ?, "updated_at" = ? WHERE "ratings"."id" = ? [["post_id", 34], ["updated_at", "2015-12-08 13:06:55.878147"], ["id", 5]]
(170.0ms) commit transaction
Redirected to http://localhost:3000/topics/9
Completed 302 Found in 489ms (ActiveRecord: 397.0ms)
将 post.id
更改为 NULL
SQL (2.0ms) UPDATE "ratings" SET "post_id" = NULL WHERE "ratings"."post_id" = ? AND "ratings"."id" IN (4, 25) [["post_id", 34]]
我不知道这是怎么发生的以及如何克服这个所以,请帮忙。
它改变评级数据库如下:
第 1 列:id,第 2 列:star,第 3 列:post_id
1,1,NULL
2,2,NULL
3,3,NULL
4,4,NULL
5,5,34
6,4,NULL
7,1,NULL
8,1,NULL
9,5,NULL
10,1,NULL
11,5,NULL
12,1,NULL
13,4,NULL
14,3,NULL
15,4,NULL
16,4,NULL
17,4,NULL
18,2,NULL
19,1,NULL
20,5,NULL
21,3,NULL
Post 型号:
class Post < ActiveRecord::Base
belongs_to :topic
has_many :comments
has_and_belongs_to_many :tags
has_many :ratings
end
评级模型:
class Rating < ActiveRecord::Base
belongs_to :post
end
Post show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @posts.name %> (
<%= @posts.topic.name %> )
</p>
<p>
<strong>Email:</strong>
<%= @posts.email %>
</p>
<p>
<strong>Message:</strong>
<%= @posts.message %>
</p>
<strong>Tags:</strong>
<% @posts.tags.each do |tag| %>
<div>
<%= tag.name %> <br>
</div>
<% end %>
<br>
<strong>Rating:</strong>
<%= @posts.ratings.group(:star).count %>
<%= form_for @posts do |f| %>
<% (1..5).each do |rating| %>
<%= radio_button_tag "post[rating_ids][]", rating %>
<%= rating %>
<% end %>
<%= f.submit('Rate') %>
<% end %>
<%= link_to 'Comments', post_comments_path(@posts) %>
<%= link_to 'Edit', edit_post_path(@posts) %> |
<%= link_to 'Back', topic_posts_url(@posts.topic) %>
Post 控制器:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
if params[:topic_id].to_i > 0
@topic = Topic.find(params[:topic_id])
@posts = @topic.posts.paginate(page: params[:page], per_page: 10)
else
@posts = Post.eager_load(:topic).paginate(page: params[:page], per_page: 10)
end
end
# GET /posts/1
# GET /posts/1.json
def show
@posts = Post.find(params[:id])
@tags = @posts.tags
@comment = Comment.new(:post => @posts)
end
# GET /posts/new
def new
@topic = Topic.find(params[:topic_id])
@posts = @topic.posts.new
end
# GET /posts/1/edit
def edit
@posts = Post.find(params[:id])
@tags = @posts.tags
end
# POST /posts
# POST /posts.json
def create
@topic = Topic.find(params[:topic_id])
@posts = @topic.posts.build(post_params)
respond_to do |format|
if @posts.save
format.html { redirect_to topic_url(@posts.topic_id), notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @posts }
else
format.html { render :new }
format.json { render json: @posts.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
@posts = Post.find(params[:id])
@tags = @posts.tags
respond_to do |format|
@posts.ratings.create(:star => params[:post][:rating_ids][0].to_i)
if @posts.update(post_params)
format.html { redirect_to topic_url(@posts.topic_id), notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @posts }
else
format.html { render :edit }
format.json { render json: @posts.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@posts.destroy
respond_to do |format|
format.html { redirect_to topic_url(@posts.topic_id), notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@posts = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:name, :email, :message, :topic_id, {tag_ids:[]}, rating_ids:[])
end
end
我是 rails 的新手,我需要在不使用任何 gem 的情况下实现它。 请帮助它如何将 post_id 更改为我在服务器日志中表示的 NULL ..
由于您在更新 post 之前保存了评分,因此将 post_params
更改为:
def post_params
params.require(:post).permit(:name, :email, :message, :topic_id, {tag_ids:[]})
end
因为您直接从参数中保存评分。
但是您应该考虑更改您的结构以使用嵌套属性之类的东西,它会自动 save/update 评级。
这是因为在您手动创建 Rating
之后会出现 @posts.update(rating_ids:[5])
,这表示此 post 的评分应该只有 id 5,而不是 5 星
此外,您确定要让未经身份验证的用户访问 post 编辑吗?