在铁轨中使用内部资源7
我是铁路的新手。我知道,将路线放置得比两个层次更深是不好的做法。但是,如果我将它们降低到两个级别,我会遇到其他错误。因此,现在我不知道如何保存深层路线。
doutes.rb
文件:
resources :posts do
resources :post_users
resources :comments
resources :tasks
resources :discussions do
resources :answers
end
end
answers_controller.rb
file:
class AnswersController < ApplicationController
before_action :authenticate_user!
before_action :set_answer, only: %i[ show edit update destroy ]
# GET /answers or /answers.json
def index
@answers = Answer.all
end
# GET /answers/1 or /answers/1.json
def show
end
# GET /answers/new
def new
@answer = Answer.new
end
# GET /answers/1/edit
def edit
end
# POST /answers or /answers.json
def create
@post = Post.find(params[:post_id])
@discussion = @post.discussions.find(params[:discussion_id])
@answer = @discussion.answers.create(answer_params)
@answer.user = current_user
if @answer.save
flash[:notice] = "Answer has been created"
redirect_to post_path(@post)
else
flash[:alert] = "Answer has not been created"
redirect_to post_path(@post)
end
end
# PATCH/PUT /answers/1 or /answers/1.json
def update
@post = Post.find(params[:post_id])
@discussion = @post.discussions.find(params[:discussion_id])
@answer = @discussion.answers.find(params[:id])
respond_to do |format|
if @answer.update(answer_params)
format.html { redirect_to post_url(@post), notice: 'Answer was successfully updated.' }
else
format.html { redirect_to post_url(@post), alert: 'Answer was not updated.' }
end
end
end
# DELETE /answers/1 or /answers/1.json
def destroy
@answer = @discussion.answers.find(params[:id])
@answer.destroy
redirect_to post_path(@post)
# respond_to do |format|
# format.html { redirect_to answers_url, notice: "Answer was successfully destroyed." }
# format.json { head :no_content }
# end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_answer
@answer = Answer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def answer_params
params.require(:answer).permit(:body)
end
end
descorgions_controller.rb
file:
class DiscussionsController < ApplicationController
before_action :authenticate_user!
before_action :set_discussion, only: %i[ show edit update destroy ]
# GET /discussions or /discussions.json
def index
@discussions = Discussion.all
end
# GET /discussions/1 or /discussions/1.json
def show
@answers = @discussion.answers.order(created_at: :desc)
end
# GET /discussions/new
def new
@discussion = Discussion.new
end
# GET /discussions/1/edit
def edit
end
# POST /discussions or /discussions.json
def create
@discussion = @post.discussions.create(discussion_params)
@discussion.user = current_user
if @discussion.save
flash[:notice] = "Discussion has been created"
redirect_to post_path(@post)
else
flash[:notice] = "Discussion has not been created"
redirect_to post_path(@post)
end
end
# PATCH/PUT /discussions/1 or /discussions/1.json
def update
@discussion = @post.discussions.find(params[:id])
respond_to do |format|
if @discussion.update(discussion_params)
format.html { redirect_to post_url(@post), notice: 'Discussion was successfully updated.' }
else
format.html { redirect_to post_url(@post), alert: 'Discussion was not updated.' }
end
end
end
# DELETE /discussions/1 or /discussions/1.json
def destroy
@discussion = @post.discussions.find(params[:id])
@discussion.destroy
redirect_to post_path(@post)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_discussion
@discussion = Discussion.find(params[:id])
end
# Only allow a list of trusted parameters through.
def discussion_params
params.require(:discussion).permit(:content)
end
end
我已经发送了ansys/formess/formes
到:
<%= render "answers/form", post: @post, discussion: discussion, answer: discussion.answers.build %>
answers/_form.html.erb
文件:
<%= form_with(model: [post, discussion, answer], class: "contents") do |form| %>
当我使用相同的代码时,我会得到“尚未创建答案”的警报。也就是说,它没有保存。 如何解决这个问题。我已经处理这个问题已经很长时间了。但是没有结果。
我的路由
如果需要:
post_discussion_answers_path GET /posts/:post_id/discussions/:discussion_id/answers(.:format) answers#index
POST /posts/:post_id/discussions/:discussion_id/answers(.:format) answers#create
new_post_discussion_answer_path GET /posts/:post_id/discussions/:discussion_id/answers/new(.:format) answers#new
edit_post_discussion_answer_path GET /posts/:post_id/discussions/:discussion_id/answers/:id/edit(.:format) answers#edit
post_discussion_answer_path GET /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#show
PATCH /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#update
PUT /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#update
DELETE /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#destroy
post_discussions_path GET /posts/:post_id/discussions(.:format) discussions#index
POST /posts/:post_id/discussions(.:format) discussions#create
new_post_discussion_path GET /posts/:post_id/discussions/new(.:format) discussions#new
edit_post_discussion_path GET /posts/:post_id/discussions/:id/edit(.:format) discussions#edit
发布
,讨论
和答案
模型:
class Post < ApplicationRecord
belongs_to :user
has_many :post_users, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :tasks, dependent: :destroy
has_many :discussions, dependent: :destroy
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :tasks
accepts_nested_attributes_for :discussions
end
class Discussion < ApplicationRecord
belongs_to :post
belongs_to :user
has_many :answers, dependent: :destroy
end
class Answer < ApplicationRecord
belongs_to :post
belongs_to :user
belongs_to :discussion
end
I am new to rails.I know that placing routes deeper than two levels is generally bad practice. But if I drop them down to two levels, I get other errors. So now I don't know how to save deep routes.
routes.rb
file:
resources :posts do
resources :post_users
resources :comments
resources :tasks
resources :discussions do
resources :answers
end
end
answers_controller.rb
file:
class AnswersController < ApplicationController
before_action :authenticate_user!
before_action :set_answer, only: %i[ show edit update destroy ]
# GET /answers or /answers.json
def index
@answers = Answer.all
end
# GET /answers/1 or /answers/1.json
def show
end
# GET /answers/new
def new
@answer = Answer.new
end
# GET /answers/1/edit
def edit
end
# POST /answers or /answers.json
def create
@post = Post.find(params[:post_id])
@discussion = @post.discussions.find(params[:discussion_id])
@answer = @discussion.answers.create(answer_params)
@answer.user = current_user
if @answer.save
flash[:notice] = "Answer has been created"
redirect_to post_path(@post)
else
flash[:alert] = "Answer has not been created"
redirect_to post_path(@post)
end
end
# PATCH/PUT /answers/1 or /answers/1.json
def update
@post = Post.find(params[:post_id])
@discussion = @post.discussions.find(params[:discussion_id])
@answer = @discussion.answers.find(params[:id])
respond_to do |format|
if @answer.update(answer_params)
format.html { redirect_to post_url(@post), notice: 'Answer was successfully updated.' }
else
format.html { redirect_to post_url(@post), alert: 'Answer was not updated.' }
end
end
end
# DELETE /answers/1 or /answers/1.json
def destroy
@answer = @discussion.answers.find(params[:id])
@answer.destroy
redirect_to post_path(@post)
# respond_to do |format|
# format.html { redirect_to answers_url, notice: "Answer was successfully destroyed." }
# format.json { head :no_content }
# end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_answer
@answer = Answer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def answer_params
params.require(:answer).permit(:body)
end
end
discussions_controller.rb
file:
class DiscussionsController < ApplicationController
before_action :authenticate_user!
before_action :set_discussion, only: %i[ show edit update destroy ]
# GET /discussions or /discussions.json
def index
@discussions = Discussion.all
end
# GET /discussions/1 or /discussions/1.json
def show
@answers = @discussion.answers.order(created_at: :desc)
end
# GET /discussions/new
def new
@discussion = Discussion.new
end
# GET /discussions/1/edit
def edit
end
# POST /discussions or /discussions.json
def create
@discussion = @post.discussions.create(discussion_params)
@discussion.user = current_user
if @discussion.save
flash[:notice] = "Discussion has been created"
redirect_to post_path(@post)
else
flash[:notice] = "Discussion has not been created"
redirect_to post_path(@post)
end
end
# PATCH/PUT /discussions/1 or /discussions/1.json
def update
@discussion = @post.discussions.find(params[:id])
respond_to do |format|
if @discussion.update(discussion_params)
format.html { redirect_to post_url(@post), notice: 'Discussion was successfully updated.' }
else
format.html { redirect_to post_url(@post), alert: 'Discussion was not updated.' }
end
end
end
# DELETE /discussions/1 or /discussions/1.json
def destroy
@discussion = @post.discussions.find(params[:id])
@discussion.destroy
redirect_to post_path(@post)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_discussion
@discussion = Discussion.find(params[:id])
end
# Only allow a list of trusted parameters through.
def discussion_params
params.require(:discussion).permit(:content)
end
end
I have sent the answers/form
to:
<%= render "answers/form", post: @post, discussion: discussion, answer: discussion.answers.build %>
answers/_form.html.erb
file:
<%= form_with(model: [post, discussion, answer], class: "contents") do |form| %>
when I use the same code, I get the alert "Answer has not been created". That is, it is not saved.
How to solve this problem. I have been dealing with this issue for a long time. But there is no result.
My route
if needed:
post_discussion_answers_path GET /posts/:post_id/discussions/:discussion_id/answers(.:format) answers#index
POST /posts/:post_id/discussions/:discussion_id/answers(.:format) answers#create
new_post_discussion_answer_path GET /posts/:post_id/discussions/:discussion_id/answers/new(.:format) answers#new
edit_post_discussion_answer_path GET /posts/:post_id/discussions/:discussion_id/answers/:id/edit(.:format) answers#edit
post_discussion_answer_path GET /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#show
PATCH /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#update
PUT /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#update
DELETE /posts/:post_id/discussions/:discussion_id/answers/:id(.:format) answers#destroy
post_discussions_path GET /posts/:post_id/discussions(.:format) discussions#index
POST /posts/:post_id/discussions(.:format) discussions#create
new_post_discussion_path GET /posts/:post_id/discussions/new(.:format) discussions#new
edit_post_discussion_path GET /posts/:post_id/discussions/:id/edit(.:format) discussions#edit
post
, discussion
and answer
models:
class Post < ApplicationRecord
belongs_to :user
has_many :post_users, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :tasks, dependent: :destroy
has_many :discussions, dependent: :destroy
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :tasks
accepts_nested_attributes_for :discussions
end
class Discussion < ApplicationRecord
belongs_to :post
belongs_to :user
has_many :answers, dependent: :destroy
end
class Answer < ApplicationRecord
belongs_to :post
belongs_to :user
belongs_to :discussion
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论