嵌套路由和 CRUD 操作以及 Rails 中关联的附加值 [英] Nested routes and CRUD operations with additional values for assciation in Rails
问题描述
我通过 rails api 中的关系创建了一个 has_many.我还使用了嵌套路由.
我的模型如下;
class Author :注释结尾课后:注释结尾班级评论ActiveRecord::Base归属地:作者归属地:发布结尾
我的路线如下;
Rails.application.routes.draw 做命名空间:api,默认值:{ :format =>'json' } 做资源:帖子做资源:评论结尾资源:作者结尾结尾
所以我的目标是
<块引用>- 评论是嵌套路由,以便我可以从帖子中创建和显示评论
- 这里不是任何帖子作者.作者是为评论所有者准备的
我实现了几乎所有的概念和工作.但是我面临以下两个关联问题
- 如何在父级创建时为关联表添加其他字段.我的要求是在创建帖子时,我需要插入一个默认条目以进行评论.我的创建后控制器实现如下
<块引用>
def 创建params = create_params.merge(:record_status => 1)@post = Post.new(params)@post.authors <<作者.find(1)如果@post.save渲染:显示,状态::创建结尾结尾高清秀@post = Post.find(params[:id])结尾私人的def create_paramsparams.permit(:name, :description, :author_id)结尾
这里我在请求 json 中传递 author_id.这需要在评论表中添加为作者 ID.现在我只是硬编码为 1.我使用了 '<<'用于关联条目.这是有效的,但我还需要包括另外两个字段:comments 和:record_status.再次:评论来自请求本身.
注意:这不是 rails mvc 应用程序.这是rails api,输入为json.
- 当我使用嵌套路由显示评论时,我需要显示作者以及评论表中的评论.我的评论控制器方法是 ;
<块引用>
class Api::CommentsController <应用控制器before_filter :fetch_post定义索引@authors = @post.authors.where(:record_status => 1, 评论:{ record_status: 1 })结尾私人的def fetch_post@post = Post.find(params[:post_id])结尾结尾
在这里我得到了作者,但在连接表评论"中没有正确的评论
请帮我解决这些问题
对于第一个问题,您要配置 posts_controller
以接受评论的嵌套属性.在控制器的开头添加这一行:
accepts_nested_attributes_for :comments
查看文档了解更多详情在这个方法上.
然后,您需要修改控制器允许的参数:
def create_paramsparams.permit(:name, :description, :author_id, comments_attributes: [ :author_id, :comments, :record_status ] )结尾
修改 comments_attributes
数组中列出的属性以匹配您的 Comment
模型的属性.
我不确定你想在第二个问题中得到什么,但也许你只需要稍微不同的查询:
@comments = Comment.where(post_id: @post.id).includes(:author)
以上将返回包括评论作者在内的评论列表.
I created one has_many through relationship in rails api. I also used nested routes.
My models like below;
class Author < ActiveRecord::Base
has_many :comments
has_many :posts, :through => :comments
end
class Post < ActiveRecord::Base
has_many :comments
has_many :authors, :through => :comments
end
class Comment < ActiveRecord::Base
belongs_to :author
belongs_to :post
end
my routes like below;
Rails.application.routes.draw do
namespace :api, defaults: { :format => 'json' } do
resources :posts do
resources :comments
end
resources :authors
end
end
So here my aims are
- Comments are nested route so that i can create and display comments from post
- Here not any post author. The author is meant for comment owner
I implemented the concepts and working almost all. But i am facing the following 2 problems with associations
- How to add additional fields for associated table when parent create. Here my requirement is when a post is created, i need to insert one default entry for comment. My post controller implementation for create is like below
def create params = create_params.merge(:record_status => 1) @post = Post.new(params) @post.authors << Author.find(1) if @post.save render :show, status: :created end end def show @post = Post.find(params[:id]) end private def create_params params.permit(:name, :description, :author_id ) end
Here i am passing author_id in the request json. This needs to be added as author id in the comments table. Now i just hard coded as 1. I used '<<' for association entry. This is working but i also need to include two more fields which are :comments and :record_status. Again :comments is from the request itself.
Note: This is not rails mvc application. This is rails api and input as json.
- When i display comments using nested routes i need to show author and also comments from comments table. My comments controller method is ;
class Api::CommentsController < ApplicationController before_filter :fetch_post def index @authors = @post.authors.where(:record_status => 1, comments: { record_status: 1 }) end private def fetch_post @post = Post.find(params[:post_id]) end end
Here i got authors but not correct comments in the join table 'comments'
Please help me to solve these issues
For the first problem, you want to configure the posts_controller
to accept the nested attributes for comments. Add this line at the beginning of your controller:
accepts_nested_attributes_for :comments
Check the documentation for further details on this method.
Then, you need to modify the parameters that the controller will allow:
def create_params
params.permit(:name, :description, :author_id, comments_attributes: [ :author_id, :comments, :record_status ] )
end
Modify the attributes listed in the comments_attributes
array to match those of your Comment
model.
I'm not sure what you're trying to get in the second problem, but maybe you just need to query a little differently:
@comments = Comment.where(post_id: @post.id).includes(:author)
The above would return a list of comments including the comment author.
这篇关于嵌套路由和 CRUD 操作以及 Rails 中关联的附加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!