2009-10-20 2 views
1

컨트롤러 내에서 action_name을 얻으려는 정보를 찾았지만 내 모델 안에 무엇이 있는지 알아야합니다. 저는 제 자신을 추측하는 일종의 일종으로, 모델에서 필요로하거나 모델에서 벗어날 수 있는지에 대해 궁금해하고 있습니다. 그러나 확실하지 않습니다. 내 모델에서 action_name과 같은 것을 얻을 수있는 방법이 있다면 알려 주시기 바랍니다.RoR에서 모델 내의 현재 작업을 얻는 방법은 무엇입니까?

+3

당신이 뭘 하려는지에 대한 자세한 정보를 제공 할 수 attr_accessible 예와 액션을 설정? 모델 내에서 컨트롤러에 액세스 할 필요는 없습니다. 어쩌면 우리는 더 깨끗한 방법을 찾도록 도울 수 있습니다. –

답변

2

엄격한 의미에서 모델에 액세스하는 컨트롤러에 대한 가시성이 없어야합니다. 즉, 나쁜 습관입니다.

어떤 경우에도 액세스하려는 경우 컨트롤러에서 모델 메서드 이름으로 '컨트롤러'개체를 전달할 수 있습니다. 여기에는 필요한 모든 정보가 들어 있습니다.

 
controller.action_name 

도 작업 이름을 제공합니다.

+0

동의합니다. 정말 나쁜 습관입니다. – ohdeargod

0

정확히 무엇을하려는 것인지 모르겠지만 모델에서 컨트롤러에 연결할 필요가있는 것처럼 으로 느껴지는 경우가있었습니다. 물론 나쁜 습관.

한편 컨트롤러에는 모델에 필요할 수있는 데이터 (예 : 현재 사용자)가 있습니다. 예를 들어 항목을 편집 한 이메일을 보낼 수 있습니다. 이 경우 현재 사용자가 아닌 경우에만 항목 작성자에게 전자 메일을 보낼 수 있습니다. 내가 편집 중이라면 이메일을 보낼 필요가 없습니다. 맞습니까?

내가 사용한 솔루션은 컨트롤러에 대한 액세스 권한이있는 스위퍼를 사용하는 것입니다.

class BugSweeper < ActionController::Caching::Sweeper 
    observe Bug 

    def after_create(record) 
     email_created(record) 
    end 

    def before_update(record) 
     email_edited(record) 
    end 

    def email_created(record, user = controller.session[:user_id]) 
     editor = find_user(user) 
     if record.is_assigned? 
      TaskMailer.deliver_assigned(record, editor) unless record.editor_is_assignee?(editor) 
     end 
    end 
end 

YMMV.

0

다른 사람들이 지적한 것처럼 이것은 매우 나쁜 습관입니다. 모델에 어떤 작업이 사용되는지 알 필요가 없습니다. 작업에 따라 달라지는 코드는 일반적으로 매우 약합니다. 또한 컨트롤러에 속한 모델에 코드를 배치하여 모델보기와 컨트롤러 사이의 선을 흐리게 만듭니다.

  1. 은 일반적인 방법을 확인하고 컨트롤러에서의 출력에 따라 행동 : 당신이 그것을 실현시키기에 죽은 세트라면

    그러나, 여기에 모델의 컨트롤러 액션을 사용하기위한 몇 가지 더 옵션이 있습니다. (표준 작업 방법)

  2. 특정 컨트롤러 컨트롤러에서만 사용되는 모델에서 메서드를 만듭니다. (매우 작음 DRY)
  3. 조치를 인수로 인수로 전달하십시오.
  4. 생성 된 메소드 attr_accessible을 사용하여 작업을 설정하십시오.

다음은 각 솔루션의 예입니다. 인자 예를 들어 동작을 전달

일반적인 방법 예

class MyModel < ActiveRecord::Base 
    ... 
    def do_stuff 
    # things. 
    end 

    def suitable_for_use_in_action? 
    # returns true if the model meets criteria for action 
    end 
end 

class MyModelsController < ApplicationController 

    ... 

    def index 
    @myModel = MyModel.find(params[:id]) 
    @myModel.do_stuff if @myModel.suitable_for_use_in_action? 
    end 
end 

특정 동작 예

class MyModel < ActiveRecord::Base 
    ... 
    def do_stuff_in_index 
    # things that should only be called if the action is index. 
    end 
end 

class MyModelsController < ApplicationController 

    ... 

    def index 
    @myModel = MyModel.find(params[:id]) 
    @myModel.do_stuff_in_index 
    end 
end 

.

class MyModel < ActiveRecord::Base 
    ... 
    def do_stuff(action) 
    if action == :index 
     # things that should only be called if the action is index. 
    else 
     #things to be done when called from all other actions 
    end 
    end 
end 

class MyModelsController < ApplicationController 

    ... 

    def index 
    @myModel = MyModel.find(params[:id]) 
    @myModel.do_stuff(:index) 
    end 
end 

class MyModel < ActiveRecord::Base 
    ... 
    attr_accessible :action 

    def do_stuff 
    if @action == :index 
     # things that should only be called if the action is index. 
    else 
     #things to be done when called from all other actions, or when called while @action is not set. 
    end 
    end 
end 

class MyModelsController < ApplicationController 

    ... 

    def index 
    @myModel = MyModel.find(params[:id]) 
    @myModel.action = :index 
    @myModel.do_stuff 
    end 
end 
관련 문제