Подтвердить что ты не робот

Маршрутная озабоченность и полиморфная модель: как поделиться контроллером и представлениями?

Учитывая маршруты:

Example::Application.routes.draw do
  concern :commentable do
    resources :comments
  end

  resources :articles, concerns: :commentable

  resources :forums do
    resources :forum_topics, concerns: :commentable
  end
end

И модель:

class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
end

Когда я редактирую или добавляю комментарий, мне нужно вернуться к "комментаризуемому" объекту. У меня есть следующие проблемы:

1) redirect_to в comments_controller.rb будет отличаться в зависимости от родительского объекта

2) Ссылки на представления также будут отличаться

= simple_form_for comment do |form|

Есть ли практический способ обмена представлениями и контроллерами для этого ресурса comment?

4b9b3361

Ответ 1

Вы можете найти родительский фильтр перед фильтром следующим образом:

comments_controller.rb

before_filter: find_parent

def find_parent
  params.each do |name, value|
    if name =~ /(.+)_id$/
      @parent = $1.classify.constantize.find(value)
    end
  end
end

Теперь вы можете перенаправить или сделать все, что угодно, в зависимости от типа родителя.

Например, в представлении:

= simple_form_for [@parent, comment] do |form|

Или в контроллере

comments_controller.rb

redirect_to @parent # redirect to the show page of the commentable.

Ответ 2

В Rails 4 вы можете передать варианты проблем. Поэтому, если вы это сделаете:

# routes.rb
concern :commentable do |options|
  resources :comments, options
end


resources :articles do
  concerns :commentable, commentable_type: 'Article'
end

Затем, когда вы rake routes, вы увидите, что у вас есть маршрут, похожий на

POST /articles/:id/comments, {commentable_type: 'Article'}

Это будет отменять все, что запрос пытается установить для обеспечения безопасности. Затем в вашем приложении "Комментарии":

# comments_controller.rb
class CommentsController < ApplicationController

  before_filter :set_commentable, only: [:index, :create]

  def create
    @comment = Comment.create!(commentable: @commentable)
    respond_with @comment
  end

  private
  def set_commentable
    commentable_id = params["#{params[:commentable_type].underscore}_id"]
    @commentable = params[:commentable_type].constantize.find(commentable_id)
  end

end

Один из способов тестирования такого контроллера с помощью rspec:

require 'rails_helper'

describe CommentsController do

  let(:article) { create(:article) }

  [:article].each do |commentable|

    it "creates comments for #{commentable.to_s.pluralize} " do
      obj = send(commentable)
      options = {}
      options["#{commentable.to_s}_id"] = obj.id
      options["commentable_type".to_sym] = commentable.to_s.camelize
      options[:comment] = attributes_for(:comment)
      post :create, options
      expect(obj.comments).to eq [Comment.all.last]
    end

  end

end