在这种情况下,可以通过注释查看post_id,但是我想从仅具有post资源ID的post constroller中获取提示, 注释中的post_id和帖子中的id是相同的ID,因此我试图将两者关联
Class RandomTest:
def __init__(self, d=None, v=None):
self.d = d or []
self.v = v or []
def add(self, d, v):
self.d.append(d)
self.v.append(v)
my_dict = {}
for x in range(100):
if str(x) not in my_dict:
my_dict[str(x)] = RandomTest()
my_dict[str(x)].add(str(x), x)
print(my_dict['1'].d)
['1']
好日志显示错误
所以,请有人为此留个小费?
def set_post
@post = Post.find(params[:id])
end
def set_comment_post
@post = Post.find_by(post_id: set_post)
end
根据请求传递的参数
class PostsController < ApplicationController
before_action :set_comment_post only: [:comments]
before_action :set_post, only: [:show, :update, :destroy], except: [:comments]
before_action :set_user, only: [:show, :update, :destroy, :new]
# GET /posts
# GET /posts.json
def comments
@comments = @post.comments.order('created_at desc')
render json: @comments
end
# POST /posts
# POST /posts.json
def create
@post = current_user.posts.build(post_params)
if @post.save
render json: "Posted successfully", status: 201
else
render json: @post.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
if @post.update(post_params)
render json: "Posted updated successfully", status: 200
else
render json: @post.errors, status: :unprocessable_entity
end
end
private
def set_user
@current_user = User.find_by(params[:id])
end
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
def set_comment_post
@post = Post.find_by(:post_id => set_post)
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body, :user_id, :posts_count)
end
end
答案 0 :(得分:0)
尚不清楚为什么需要使用两种方法做同样的事情,但我认为您想要的是
如果多个端点需要它,请仅使用before_action
。另外,请勿只使用[:only]
和[:except]
两者。这应该起作用:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :update, :destroy, :comments]
before_action :set_user, only: [:show, :update, :destroy, :new]
def comments
@comments = @post.comments.order('created_at desc')
render json: @comments
end
def create
@post = current_user.posts.build(post_params)
if @post.save
render json: "Posted successfully", status: 201
else
render json: @post.errors, status: :unprocessable_entity
end
end
def update
if @post.update(post_params)
render json: "Posted updated successfully", status: 200
else
render json: @post.errors, status: :unprocessable_entity
end
end
private
def set_user
@current_user = User.find_by(params[:id])
end
def set_post
@post = Post.find(params[:id])
end
def post_params
# add comments attributes if our text field is called 'text'
params.require(:post).permit(:title, :body, :user_id, :posts_count, comments_attributes: [ :id, :text ] )
end
end
您可能还需要设置accepts_nested_attributes_for
class Post
accepts_nested_attributes_for :comments, allow_destroy: true
has_many :comments, dependent: :destroy
end
然后,您要在前端处理数据,您需要传递带有嵌套字段的帖子表格以供注释。参见https://guides.rubyonrails.org/form_helpers.html#nested-forms