2013-12-22 1 views
0

컨트롤러의 show 액션 내에 양식을 작성하려고합니다. 여기에 내 설정입니다. 구독 네임 스페이스 사용외부 모델에 대한 show 액션 내부에 양식 작성

분류 모델

class Category < ActiveRecord::Base 

    # Associations 
    has_many :feeds 
    has_many :subscriptions 

    # Nested attributes 
    accepts_nested_attributes_for :subscriptions 

end 

서브 스크립 션 모델

class Subscription < ActiveRecord::Base 

    # Associations 
    belongs_to :profile 
    belongs_to :category 
    belongs_to :feed 

end 

: 범주 (http://URL.com/subscriptions/categories/1)의 show 액션 내부

# Subscriptions 
    namespace :subscriptions do 
    resources :categories 
    end 

, 난을 구축하려는 구독 모델 양식을 만들고 해당 카테고리에 사용할 수있는 피드를 나열하십시오. 체크 된 피드를 구독 모델에 제출하는 양식을 원합니다. 그것은 적절한 정보를 렌더링하고 지금

<%= simple_form_for [:subscriptions, @category] do |f| %> 
    <%= f.association :feeds, collection: @feeds, value_method: :id, as: :check_boxes %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

을하지만, 편집 형태로 렌더링되는 : 범주의 표시 행동 양식을 구축을 위해 노력

CategoriesController

def show 
    @feeds = Feed.where("category_id = ?", @category) 
    end 

보기 , 작성 양식이 아닙니다. 이 문제를 어떻게 해결할 수 있습니까?

내 루트/컨트롤러에 추가해야 실제로 구독 정보 모델에 정보를 저장해야합니까?

감사합니다.

답변

0

첫 번째 것은 어디에 표시 액션에서 @category 개체를 초기화하는지 모르겠습니다. 새 페이지를 렌더링 할 경우 컨트롤러의 새로운 액션을지도해야하며 당신이 가진 경로 가

http://URL.com/subscriptions/categories/1 

항상 쇼에 매핑합니다

http://URL.com/subscriptions/categories/new 

당신이

를 사용하는 경로 것보다 레일 컨벤션에 따라 액션을 수행 할 수 있지만 그에 맞게 오버라이드 할 수는 있지만 좋은 방법은 아닙니다. 또한 새 작업에서

@category = Category.new 

에 인스턴스를 초기화하면 자동으로 생성 작업으로 넘어갑니다. 당신의 @category 이미

@category = Category.find(1) 

예를

에 대한 데이터베이스에 유지되는 경우이 객체가 simple_form_for에 가면 지금은 조치를 업데이트 할 데이터를 전송합니다.