简体   繁体   English

在Rails中创建回复关联

[英]Create reply association in rails

So I'm working on a project where a User can create a Topic and users can reply to them. 因此,我正在一个项目中,用户可以创建主题,并且用户可以回复主题。 I'm having a great deal of trouble figuring out how to associate the user_id and topic_id and store them when a reply is made. 我在弄清楚如何将user_id和topic_id关联起来并在回复时存储它们方面遇到了很多麻烦。

Model Reply.rb 模型Reply.rb

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

Controller replies_controller.rb 控制器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

Controller topics_controller.rb 控制器topic_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

View topic/show 查看主题/节目

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

Routes routes.rb 路线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

Why are you treating replies as a separate dataset? 为什么将replies作为单独的数据集处理? You'd be best keeping all the topics under one model , which you'll be able to separate using a hierarchy gem such as acts_as_tree : 您最好将所有topics放在一个model ,您可以使用诸如acts_as_tree类的层次结构gem将其分开:

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

This way, you'll be able to use something like the following: 这样,您将可以使用以下内容:

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

This way, you'll be able to use the following: 这样,您将可以使用以下内容:

#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 %>

This will allow you to create new topic records for each reply, negating any need for a Reply model. 这样,您就可以为每个答复创建新的主题记录,而无需任何Reply模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM