所以我正在開發一個項目,用戶可以創建一個主題,用戶可以回覆它們。我在確定如何關聯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
你可以添加你的路線請 –
@MichalSzyndel獲得「用戶/秀」 資源(僅有意義的!):用戶 資源:故事 資源:主題 資源:回覆 – cookiemonster
請把它添加到這個問題,格式化它,看在上帝的份上! –