Ruby on Rails 3.2.2를 사용하고 있는데 컨트롤러 액션을 다른 컨트롤러 액션에 "맵핑"할 수 있는지 알고 싶지만 일부 파라미터는 변경하고 싶습니다.컨트롤러 동작을 다른 컨트롤러 동작에 "매핑"할 수 있습니까?하지만 일부 매개 변수는 변경됩니까?
# File system:
# /app/models/articles/user_association.rb
# /app/models/users/article_association.rb
# /app/controllers/users/article_associations_controller.rb
# /app/controllers/articles/user_associations_controller.rb
# /app/models/articles/user_association.rb
class Articles::UserAssociation < ActiveRecord::Base
...
end
# /app/models/users/article_association.rb
class Users::ArticleAssociation < Articles::UserAssociation # Note inheritance
#none
end
# /app/controllers/users/article_associations_controller.rb
class Articles::UserAssociationsController < ApplicationController
def show
@articles_user_association = Articles::UserAssociation.find(params[:article_id])
...
end
def edit
@articles_user_association = Articles::UserAssociation.find(params[:article_id])
...
end
...
end
# /app/controllers/articles/user_associations_controller.rb
class Users::ArticleAssociationsController < ApplicationController
def show
# It is the same as the Articles::UserAssociationsController#show
# controller action; the only thing that changes compared to
# Articles::UserAssociationsController#show is the usage of
# 'params[:user_id]' instead of 'params[:article_id]'.
@users_article_association = Users::ArticleAssociation.find(params[:user_id])
...
end
def edit
# It is the same as the Articles::UserAssociationsController#edit
# controller action; the only thing that changes compared to
# Articles::UserAssociationsController#edit is the usage of
# 'params[:article_id]' instead of 'params[:user_id]'.
@users_article_association = Users::ArticleAssociation.find(params[:user_id])
...
end
...
end
그래서, 나는 /articles/:article_id/user
경로와 관련된 컨트롤러 액션으로 /users/:user_id/article
경로로 이동 HTTP 요청을 처리하고 싶습니다 : 그건 내가 다음 모델과 컨트롤러를 가지고있다.
주 : 이전에 말했듯이 나는, (자신을 반복하지 마십시오) 코드를 건조하기 위해 그것을하기 위해 좋아하지만 것, Users::ArticleAssociationsController
및 Articles::UserAssociationsController#show
사이에서 변경 유일한 것은 params
입니다.
가능합니까?
실제로 'Users :: ArticleAssociation'과'Articles :: UserAssociation'은 클래스 상속 이후 * same *입니다. 그러나 해결책은 옳을 수 있습니다. – Backo
'find' 메소드는 어떤 종류의 ID를 얻는 지 어떻게 압니까? 두 클래스가 같은 경우에는 숫자 만 전달하고, 원하는 경우 ID는 모든 경우에 사용자 ID 또는 기사 ID로 사용됩니다. – Matzi