Ruby on rails 基于主题的Rails URL路由和分组帖子
我是rails新手,我使用scaffold创建了post模型并发布了名为string、EMail:string、Message:text、topic\u id:integer列的控制器 我还创建了一个主题模型和主题控制器,其中包含主题名称:string 我提供了模型之间的关系,如下所示:Ruby on rails 基于主题的Rails URL路由和分组帖子,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我是rails新手,我使用scaffold创建了post模型并发布了名为string、EMail:string、Message:text、topic\u id:integer列的控制器 我还创建了一个主题模型和主题控制器,其中包含主题名称:string 我提供了模型之间的关系,如下所示: class Topic < ActiveRecord::Base has_many :posts, foreign_key: 'topic_id' end class Post <
class Topic < ActiveRecord::Base
has_many :posts, foreign_key: 'topic_id'
end
class Post < ActiveRecord::Base
belongs_to :topic
end
主题\u controller.rb代码:
我需要使用主题对帖子进行分组。i、 例如,单击某个特定主题上的显示,它应该转到URL/topics//posts,在那里它应该列出与该主题相关的所有帖子,我可以创建/删除属于该主题的帖子
有人能帮忙吗。。
谢谢。您的问题应该更直接,有很多信息与问题无关,例如属性名称,您的目标不够明确 看来你只是想设置路线,对吧?通过关联:topic.posts,您已经拥有与该主题相关的所有帖子。您只需为帖子设置路由:
resources :topics do
resources :posts
end
此外,由于您使用的是命名约定,因此不需要使用foreign_key选项。似乎您还可以用大写字母命名一些属性,它们应该是name、email和message
更新:
在索引操作中,由于您希望帖子属于一个主题,因此需要确定@posts实例变量的范围。因为您使用的是嵌套资源,所以您有参数params[:topic_id],所以只需使用@topic=topic.findparams[:topic_id]获取主题,然后使用@posts=@topic.posts确定关联范围。您需要对其他每个操作执行相同的操作。我建议您阅读一下Rails中的关联,您可能需要使用@topic.posts.build和@topic.posts.findparams[:id]等方法。我在这个链接中找到了解决此问题的方法:
下载源代码并找到解决方案…当我单击“显示”时,您能告诉我这方面的代码吗?它只显示URL topics/topic\u id中的主题名称
class TopicsController < ApplicationController
before_action :set_topic, only: [:show, :edit, :update, :destroy]
# GET /topics
# GET /topics.json
def index
@topics = Topic.all
end
# GET /topics/1
# GET /topics/1.json
def show
end
# GET /topics/new
def new
@topic = Topic.new
end
# GET /topics/1/edit
def edit
end
# POST /topics
# POST /topics.json
def create
@topic = Topic.new(topic_params)
respond_to do |format|
if @topic.save
format.html { redirect_to @topic, notice: 'Topic was successfully created.' }
format.json { render :show, status: :created, location: @topic }
else
format.html { render :new }
format.json { render json: @topic.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /topics/1
# PATCH/PUT /topics/1.json
def update
respond_to do |format|
if @topic.update(topic_params)
format.html { redirect_to @topic, notice: 'Topic was successfully updated.' }
format.json { render :show, status: :ok, location: @topic }
else
format.html { render :edit }
format.json { render json: @topic.errors, status: :unprocessable_entity }
end
end
end
# DELETE /topics/1
# DELETE /topics/1.json
def destroy
@topic.destroy
respond_to do |format|
format.html { redirect_to topics_url, notice: 'Topic was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_topic
@topic = Topic.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def topic_params
params.require(:topic).permit(:Name)
end
end
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, 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
@post = 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)
end
end
resources :topics do
resources :posts
end