2015-09-28 70 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 下載源代碼,並找到解決辦法......