2015-11-08 84 views
1

所以我正在開發一個項目,用戶可以創建一個主題,用戶可以回覆它們。我在確定如何關聯user_id和topic_id並在做出回覆時將其存儲起來非常麻煩。在rails中創建回覆關聯

型號Reply.rb

class Reply < ActiveRecord::Base 
    belongs_to :topic 
    belongs_to :user 
end 

控制器replies_controller.rb

class RepliesController < ApplicationController 
    before_action :authenticate_user!, except: [:show] 

    def show 
    end 

    def new 
    @topic = Topic.find(params[:id]) 
    @reply = @topic.replies.build 
    end 

    def create 
    @reply = Reply.new(reply_params) 
    @reply.user_id = current_user.id 
    @reply.topic_id = Topic.find(params[:id]) 

    if @reply.save 
     flash[:notice] = 'Reply Success!' 
     redirect_to @reply 
    else 
     flash[:notice] = 'Response could not be made!' 
     render 'new' 
    end 
    end 

    def edit 
    end 

    def update 
    if @reply.update(reply_params) 
     flash[:notice] = 'Response updated!' 
     redirect_to @reply 
    else 
     flash[:notice] = 'Response could not be updated!' 
     redirect 'edit' 
    end 
    end 

    def destroy 
    @reply.destroy 
    flash[:notice] = 'Response removed!' 
    redirect_to replies_topic 
    end 

    private 

    def reply_params 
    params.require(:reply).permit(:details, :user_id, :topic_id) 
    end 

    # Verify so people won't be able to delete/edit other peoples replies 
    def reply_owner 
    unless @reply.user_id == current_user.id 
     flash[:notice] = 'Access denied. You are not the owner of this response.' 
     redirect_to stories_path 
    end 
    end 
end 

控制器topics_controller.rb

class TopicsController < ApplicationController 
    before_action :set_topic, only: [:show, :edit, :update, :destroy] 

    def index 
    @topics = Topic.all 
    end 

    def show 
    @topic = Topic.find(params[:id]) 
    @reply = @topic.replies 
    end 

    def new 
    @topics = Topic.new 
    end 

    def create 
    # Need system that will check if a names are similar. 
    # Similar to how stackoverflow does asking questions 

    @topics = Topic.new(topic_params) 

    if @topics.save 
     flash[:notice] = "Topic created!" 
     redirect_to @topics 
    else 
     flash[:alert] = "Topic is already created" 
     redirect 'new' 
    end 
    end 

    def edit 
    end 

    def update 
    if @topics.update(topic_params) 
     flash[:notice] = 'Topic Updated!' 
     redirect_to @topics 
    else 
     flash[:alert] = 'Topic could not be updated' 
     render 'edit' 
    end 
    end 

    def destroy 
    @topics.destroy 
    flash[:notice] = 'Topic has been removed.' 
    redirect_to topics_path 
    end 

    private 

    def topic_params 
    params.require(:topic).permit(:name) 
    end 

    def set_topic 
    @topics = Topic.find(params[:id]) 
    rescue ActiveRecord::RecordNotFound 
    flash[:alert] = 'The story you are looking for could not be 
        found. Maybe contact the author to check if they have a copy?' 
    redirect_to topics_path 
    end 
end 

查看主題/顯示

<p><%= link_to "Reply", new_reply_path(topic_id: @topic.id) %></p> 

路線的routes.rb

Rails.application.routes.draw do 

    get 'users/show' 

    root 'static_pages#home' 
    get 'about' => 'static_pages#about' 

    devise_for :users, :skip => [:sessions] 
    as :user do 
    get 'login' => 'devise/sessions#new', :as => :new_user_session 
    post 'login' => 'devise/sessions#create', :as => :user_session 
    delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session 
    end 

    resources :users 

    resources :stories 

    resources :topics 
    resources :replies 

end 
+0

你可以添加你的路線請 –

+0

@MichalSzyndel獲得「用戶/秀」 資源(僅有意義的!):用戶 資源:故事 資源:主題 資源:回覆 – cookiemonster

+0

請把它添加到這個問題,格式化它,看在上帝的份上! –

回答

0

你爲什麼治療replies作爲一個單獨的數據集?你最好保持所有topics下一個model,你就可以使用一個層次的寶石如acts_as_tree分離:

#app/models/topic.rb 
class Topic < ActiveRecord::Base 
    acts_as_tree #-> requires you put a "parent_id" column in your topics model 
end 

這樣,您就可以使用類似以下:

#config/routes.rb 
resources :topics do 
    resources :replies, controller: :topics, only: [:create, :destroy] #-> url.com/topics/:topic_id/replies 
end 

這樣,您就可以使用以下命令:

#app/controllers/topics_controller.rb 
class TopicsController < ApplicationController 
    def show 
    @topic = Topic.find params[:id] 
    @reply = @topic.children.new 
    end 

    def create 
    @topic = Topic.new topic_params 
    @topic.save 
    end 

    private 

    def topic_params 
     params.require(:topic).permit(:topic, :params, :parent_id) 
    end 
end 

#app/views/topics/show.html.erb 
<%= @topic.name %> 
<%= render "reply", collection: @topic.children, as: :reply if @topic.children.any? %> #-> these are your replies 
<%= render "new_reply" %> 

#app/views/topics/_reply.html.erb 
<%= reply.title %> 

#app/views/topics/_new_reply.html.erb 
<%= form_for @reply do |f| %> 
    <%= f.text_field :topic 
    <%= f.submit %> 
<% end %> 

這將允許你創建新主題記錄每個回覆,否定任何需要Reply模型。

+0

等待,在我的模型裏面我應該:[act_as_tree reply:「details」]? 和[reply.title]? 並看看控制器中的參數 – cookiemonster

+0

而且我將它作爲一個數據集,因爲只有用戶可以回覆一個主題,所以我覺得需要一個關聯 – cookiemonster

+0

我會將它們放入具有層次結構的相同模型中。如果你不想,那是你的選擇:) –