简体   繁体   English

带有另一个回复模型的Rails多态帖子导致路由问题

[英]Rails polymorphic posts with another reply model cause routing issue

I have a polymorphic model Post that make user able to leave a comment on different models. 我有一个多态模型Post,使用户能够在不同模型上发表评论。

and users are able to reply those Post in a Reply model which has text content only 并且用户能够以仅包含文本内容的“回复”模型回复那些帖子

It has no problem if a user want to leave a post on either Category or Subject page.but, if a user want to reply the post, it cause routing problem. 用户希望在``类别''或``主题''页面上留下帖子都没有问题,但是,如果用户想要回复帖子,则会导致路由问题。

Here are 5 models (Category /Subject /Post /Reply /User) 这是5个模型(类别/主题/发布/回复/用户)

The code are as following 代码如下

Models 楷模

models/category.rb
class Category < ActiveRecord::Base
  has_many :subjects
  has_many :posts, as: :postable
end

models/subject.rb
class Subject < ActiveRecord::Base
  belongs_to :category
  has_many :posts, as: :postable
end

models/post.rb
class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :postable, polymorphic: true
  has_many :replies
end

models/reply.rb
class Reply < ApplicationRecord
  belongs_to :user
  belongs_to :post
end

And the controllers: 和控制器:

controller/categories_controller.rb
 def show
    @postable = @category
    @post = Post.new
    @reply = Reply.new
 end

controller/subjects_controller.rb
 def show
    @postable = @subject
    @post = Post.new
    @reply = Reply.new
 end

controller/posts_controller.rb
def create
   @post = @postable.posts.build(post_params)
   @post.user = current_user

  if @post.save
     @post.reload
     redirect_to @postable, notice: "Created"
  else
     render :new
  end
end

controller/replies_controller.rb
  def create
    @reply = @post.replies.build(reply_params)
    @reply.user = current_user     

    if @reply.save
      redirect_to :back
    else
      flash[:alert] = "Sorry, something went wrong."
      render user_path
    end
  end

views: 意见:

views/XXX/show.html.erb
 ....some content of Category or Subject show page...
       <% @posts.each do |post|%>
        <%= link_to post.user.username, post.user %>
        <%= simple_format post.content %>
        <%= render partial: "replies/reply", :locals => {:post => post} %>
       <% end %>  

 view/replies/_reply.html.erb
  <% if post.replies %>
    <% post.replies.each do |reply| %>
      <%= link_to reply.user.username, reply.user %>
      <%= simple_format reply.content %>
    <% end %>
  <% end %>   
  <%= simple_form_for :reply do |r| %>
    <%= r.hidden_field :post_id, value: post.id %>
    <%= r.input :content, label: false  %>   
    <%= r.button :submit, "Submit" %>          
  <% end %>  

I tried several ways in route.rb file 我在route.rb文件中尝试了几种方法

either
  resources :subjects do
    resources :posts do
      resources :replies
    end
  end  

or
resources :replies 

Once I submit a new reply of a post, it always shows the following result 我提交新的帖子回复后,它总是显示以下结果

No route matches [POST] "/subjects/82" 没有路由匹配[POST]“ / subjects / 82”

My question is what should I do on route.rb or other file to solve the problem? 我的问题是如何在route.rb或其他文件上解决该问题?

You need to pass url with simple_form_for explicitly like this: - 您需要使用simple_form_for显式传递url ,如下所示:-

  <%= simple_form_for :reply, :url => your_create_reply_path, :method => :post  do |r| %>
    <%= r.hidden_field :post_id, value: post.id %>
    <%= r.input :content, label: false  %>   
    <%= r.button :submit, "Submit" %>          
  <% end %>   

you can check url for create reply by command rake routes | grep replies 您可以通过命令rake routes | grep replies检查url以创建回复rake routes | grep replies rake routes | grep replies and also provide method type get or post rake routes | grep replies并提供方法类型get or post

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

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