2015-09-28 29 views
-1

我是新来的rails我创建了post模型和posts_controller其中有Name:string, EMail:string, Message:text, topic_id:integer列使用脚手架。基于主题的Rails URL路由和分组帖子

我还创建了topic模型和topics_controller其中有Topic_Name:string

我提供的模型之间的关系如下:

class Topic < ActiveRecord::Base 
    has_many :posts, foreign_key: 'topic_id' 
end 

class Post < ActiveRecord::Base 
    belongs_to :topic 
end 

routes.db我创建嵌套资源为:

resources :topics do 
    resources :posts 
end 

topics_controller.rb代码:

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 

posts_controller代码:

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 

我需要使用该主题对帖子进行分组。即在对特定主题单击show时,它应该转到网址/topics/<topic_id>/posts,它应列出与该主题相关的所有帖子,并且可以创建/删除属于该主题的帖子。

谁能帮助做这个.. 谢谢。

回答

1

你的问题应该是更直接的,有很多的信息不相关的问题(属性名称,例如),和你的目标不够清楚。

看来你只是想设置路线,对不对?您已经拥有与该主题相关的所有帖子,通过关联:topic.posts。你只需要设置职位nested resource路线:

resources :topics do 
    resources :posts 
end 

而且,你不因为你使用的命名约定需要foreign_key选项。看起来你用大写命名了一些属性,它们应该是name,emailmessage

UPDATE:

index动作,因为你要属于一个主题无关的帖子,你需要范围@posts实例变量。由于您使用的是嵌套资源,因此您有参数params[:topic_id],因此只需使用@topic = Topic.find(params[:topic_id])获取主题,然后将范围与@posts = @topic.posts关联。您需要为其他操作执行相同的操作。我建议你阅读一些Rails中的关联,你可能需要使用像@topic.posts.build@topic.posts.find(params[:id])这样的方法。

+0

你能给告诉该代码,当我点击'show'它只是显示URL中的主题名称'topic/topic_id' –

+0

什么是您的控制器代码?你的视图代码? – mrodrigues

+0

现在,我编辑了我的问题,添加了控制器代码.. –

1

我发现在这个环节这个问题的解决方案:Nested resources 下载源代码,并找到解决办法......