【问题标题】:Rails How to set multiple polymorphic in controller?Rails 如何在控制器中设置多个多态?
【发布时间】:2018-08-22 22:17:30
【问题描述】:

Rails 如何在控制器中设置多个多态?

部分代码如下:

question.rb

has_many :comments, :as => :commentable, :dependent => :destroy

comment.rb

belongs_to :commentable, polymorphic: true

has_many :reports, :as => :reportable, :dependent => :destroy

report.rb

belongs_to :reportable, polymorphic: true

routes.rb

resources :questions do

  resources :comments do 

    resources :reports

end

然后在_comment.html.erb:

<%= link_to "<i class='fa fa-times mr2 fa-fw'></i>Report".html_safe,new_question_comment_report_path(@question,@comment),remote: true %>

我的报告控制器.rb

class ReportsController < ApplicationController
  before_action :logged_in_user
  before_action :set_reportable

  def new
    @report = @reportable.reports.new
    respond_to do |format|
      format.html do
        redirect_to @reportable
      end
      format.js
    end
  end

  def create
    @report = @reportable.reports.new report_params
    @report.reporter_id = current_user.id
    if @report.save
      redirect_to @reportable
    end
  end

  def destroy
    @report = Report.find(params[:id])
    @report.destroy
  end

  private
  def set_reportable
    if params[:comment_id]
      @question = Question.find(params[:question_id])
      @reportable = @question.comments.find(params[:comment_id])
    end
  end

  def report_params
    params.require(:report).permit(:content,:radio_content)
  end
end

new.js.erb 报告

$("body").append("<%= j render "reports/reports" %>")
$("#tip-offs").modal('show')

我的_reports.html.erb

<div class="modal tip-offs in" id="tip-offs" >
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button class="close" data-dismiss="modal" type="button">
          <span aria-hidden="true">&times;</span> <span class="sr-only">Close</span>
        </button>
        <h4 class="modal-title">
          <span data-model="action">Report</span>
        </h4>
      </div>
      <%= form_for [@reportable,@report] do |f| %>
      <div class="modal-body">
          <div class="pbt5">
            <%= f.text_area :content,row:3, class: "form-control",placeholder: "report more" %>
          </div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-default" data-dismiss="modal" type="button">Close</button>
        <%= f.submit "report",class:"btn btn-primary" %>
      </div>
        <% end %>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>

如果评论中的报告url如:

/questions/6/comments/15/reports/new

我得到错误:

Processing by ReportsController#new as JS
  Parameters: {"question_id"=>"6", "comment_id"=>"15"}
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Question Load (0.2ms)  SELECT  `questions`.* FROM `questions` WHERE `questions`.`id` = 10 LIMIT 1
  Comment Load (0.2ms)  SELECT  `comments`.* FROM `comments` WHERE `comments`.`commentable_id` = 6 AND `comments`.`commentable_type` = 'Question' AND `comments`.`id` = 15 LIMIT 1
  Rendering reports/new.js.erb
  Rendered reports/_reports.html.erb (24.4ms)
  Rendered reports/new.js.erb (25.8ms)
Completed 500 Internal Server Error in 38ms (ActiveRecord: 0.7ms)

NoMethodError - undefined method `comment_reports_path' for #<#<Class:0x00007fb020559858>:0x00007fb022d02c38>
Did you mean?  comment_like_tag:
  app/views/reports/_reports.html.erb:13:in `_app_views_reports__reports_html_erb___1071799134587169621_70197237478960'
  app/views/reports/new.js.erb:1:in `_app_views_reports_new_js_erb___4579025611719430071_70197237521140'

我想也许我在reports_controller.rb 的set_reportable 中的set_reportable 中有错误,如果是,我怎样才能获得正确的报告网址?

非常感谢!

【问题讨论】:

  • 能否包含错误的完整堆栈跟踪?
  • 感谢您的帮助,@JagdeepSingh,我添加了有关我的问题的更多信息,您能看看并给我一些建议吗?

标签: ruby-on-rails ruby-on-rails-5


【解决方案1】:

NoMethodError - 未定义的方法 `comment_reports_path' 用于

你的comments和reports都嵌套在questions下,所以你需要改变

<%= form_for [@reportable,@report] do |f| %>

到

<%= form_for [@question,@reportable,@report] do |f| %>

【讨论】:

  • 感谢您的帮助,@Pavan,这就是您的代码。但其实report是多态的,我希望`_reports.html.web`不仅可以用在有问题的地方,还可以用在其他控制器上,比如article等等。那么有什么方法可以让我在reports_controller.rb 中获得正确的new_question_comment_report_path,而不是更改form_for?
【解决方案2】:

您的 cmets 路径嵌套在您的问题路径下。

resources :questions do
  resources :comments do 
    resources :reports
end

但是您的表单没有提供构建 URL 的问题实例:

<%= form_for [@reportable,@report] do |f| %>

这个[@reportable, @report] 扩展为comment_reports_path,它不存在。

您还需要传递问题实例:[@question, @reportable, @report],它将扩展为 question_comment_reports_path。

如果您运行rake routes(在Rails 5 中可能是rails routes,不确定),您应该会看到所有路线及其路径的列表。它使推理现有路线及其所需的参数变得更加容易。

【讨论】:

  • 感谢您的帮助,@Mohamad,就像您的代码一样。但其实报表是多态的,我希望`_reports.html.web`不仅可以用在有问题的地方,也可以用在其他的控制器上,比如文章等。那么有什么方法可以让我在reports_controller.rb中获得正确的new_question_comment_report_path,而不是改变form_for?
  • OK,那么需要在控制器中确定父关联。有时间我会更新我的答案。
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-19
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多