2012-10-25 2 views
0

나는 분류로 블로그를 만들고 있습니다. 나는 형식으로 분류를 구현하는 방법에 조금 붙어있다. 설정을 통해 많은 관계를 맺고 여러 범주의 블로그를 연결하는 확인란을 추가하려고합니다. 지금까지 내가보기에 카테고리를 지나쳐서 나열 할 수는 있지만 어떤 이유로 든 form_for 메소드가 작동하지 않습니다.학습 레일, 블로그 카테고리를 만들려고 시도합니다

여기 내 코드입니다.

블로그 모델

class Blog < ActiveRecord::Base 
    attr_accessible :body, :title, :image 
    has_many :categorizations 
    has_many :categories, through: :categorizations 
    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" } 

    validates :title, :body, :presence => true 

end 

분류 모델

class Category < ActiveRecord::Base 
    has_many :categorizations 
    has_many :blogs, through: :categorizations 
    attr_accessible :name 
end 

분류 모델

class Categorization < ActiveRecord::Base 
    attr_accessible :blog_id, :category_id 
    belongs_to :blog 
    belongs_to :category 
end 

블로그 새로운 컨트롤러

def new 
    @blog = Blog.new 
    @categories = Category.all 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @blog } 
    end 
    end 
나는 내가 현재 배우고 바로 이후의 모든 접근하고 긍정적 인 모르겠습니다 만

블로그 새로운 형태의보기

<%= form_for(@blog, :url => blogs_path, :html => { :multipart => true }) do |f| %> 
    <% if @blog.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@blog.errors.count, "error") %> prohibited this blog from being saved:</h2> 

     <ul> 
     <% @blog.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <div class="field"> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </div> 
    <div class="field"> 
    <%= f.file_field :image %> 
    </div> 
    <div class="field"> 
    <%= f.label :body %><br /> 
    <%= f.text_area :body %> 
    </div> 
    <div class="field"> 
    Categories: 
    <% @categories.each do |category| %> 
     <% fields_for "blog[cat_attributes][]", category do |cat_form| %> 
     <p> 
      <%= cat_form.check_box :name %> 
     </p> 
     <% end %> 
    <% end %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

이 코드는 내 실패 지점

<% @categories.each do |category| %> 
      <% fields_for "blog[cat_attributes][]", category do |cat_form| %> 
      <p> 
       <%= cat_form.check_box :name %> 
      </p> 
      <% end %> 
     <% end %> 

입니다. 이것을 달성하는 방법에 대한 조언. 당신이 여기에 설명하지 않은 사용하는 경우가 아니라면

감사합니다, 모두의 CG

답변

0

먼저, 당신은 아마 별도의 Categorization 모델이 필요하지 않습니다.

class CreateBlogsCategories < ActiveRecord::Migration 
    def change 
    create_table :blogs_categories, id: false do |t| 
     t.references :blog 
     t.references :category 
    end 
    end 
end 

그런 다음이 같은 뷰를 구성 할 수 있습니다 :

<div class="field"> 
    Categories: 
    <% @categories.each do |category| %> 
    <%= label_tag do %> 
     <%= check_box_tag 'blog[category_ids][]', category.id, @blog.category_ids.include?(category.id) %> 
     <%= category.name %> 
    <% end %> 
    <% end %> 
</div> 

class Blog < ActiveRecord::Base 
    has_and_belongs_to_many :categories 
end 

class Category < ActiveRecord::Base 
    has_and_belongs_to_many :blogs 
end 

당신은이 같은 데이터베이스 테이블을 가져야한다 :이 같은 다 대다 관계를 설정할 수 있습니다

마지막으로 에 url: blogs_path을 지정합니다.이 양식을 edit 작업에 사용하려는 경우이 항목을 삭제해야합니다. PUT 요청을 /blogs/:id으로 신고하십시오. routes.rb에서 resources :blogs을 사용했다고 가정하면 레일스는 양식 렌더링에 사용 된 작업에 따라 올바른 경로를 결정합니다.

+0

감사합니다. 작동하는 것 같습니다. 레일 캐스트가 말했듯이, 많은 것을 소유하고 있다고 많은 사람들이 알고있는 방식을 사용하고있었습니다. 많은 것들이 이런 종류의 일을하는 덜 인기있는 방법입니다. 그러나 폼 로직은 두 가지 모두에서 작동하는 것처럼 보입니다. 도와 주셔서 감사합니다 :) –

관련 문제