2012-02-25 20 views
6

Tôi đang cố gắng để tạo ra thẻ bài bằng cách làm theo các Rails Guide:Không thể chỉ định khối lượng thuộc tính được bảo vệ: tags_attributes?

tag.rb:

class Tag < ActiveRecord::Base 
    attr_accessible :name 

    belongs_to :post 
end 

post.rb:

class Post < ActiveRecord::Base 
    attr_accessible :title, :content, :tags 

    validates :title, :presence => true, 
         :length => { :maximum => 30 }, 
         :uniqueness => true 
    validates :content, :presence => true, 
         :uniqueness => true 

    belongs_to :user 

    has_many :comments, :dependent => :destroy 
    has_many :votes, :as => :votable, :dependent => :destroy 
    has_many :tags 

    accepts_nested_attributes_for :tags, :allow_destroy => :true, 
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } 
end 

views/bài viết /_form.html.erb:

<% @post.tags.build %> 
<%= form_for(@post) do |post_form| %> 
    <%= render 'shared/error_messages' %> 
    <div class="field"> 
    <%= post_form.label :title %><br /> 
    <%= post_form.text_field :title %> 
    </div> 
    <div class="field"> 
    <%= post_form.label :content %><br /> 
    <%= post_form.text_area :content %> 
    </div> 
    <h2>Tags</h2> 
    <%= render :partial => 'tags/form', 
      :locals => {:form => post_form} %> 
    <div class="actions"> 
    <%= post_form.submit %> 
    </div> 
<% end %> 

views/thẻ/_form.html.erb:

<%= form.fields_for :tags do |tag_form| %> 
    <div class="field"> 
    <%= tag_form.label :name, 'Tag:' %> 
    <%= tag_form.text_field :name %> 
    </div> 
    <% unless tag_form.object.nil? || tag_form.object.new_record? %> 
    <div class="field"> 
     <%= tag_form.label :_destroy, 'Remove:' %> 
     <%= tag_form.check_box :_destroy %> 
    </div> 
    <% end %> 
<% end %> 

Nhưng tôi nhận được lỗi này khi tôi cố gắng tạo ra các thẻ:

Không thể hàng loạt assign bảo vệ thuộc tính : tags_attributes Rails.root: /home/alex/rails/r7

Trace ứng dụng | Khung Trace | Full vết app/controllers/posts_controller.rb: 25: trong `tạo' Yêu cầu

Tham số:

{ "utf8"=> "✓", "authenticity_token"=> "VF/qlfZ4Q5yvPY4VIbpFn65hoTAXdEa4fb4I1Ug4ETE =" , "post" => {"title" => "post number 5", "content" => "post number 5 post số 5 post number 5", "tags_attributes" => {"0" => {" tên "=>" thực phẩm, đồ uống "}}}," cam kết "=>" Tạo bài đăng "}

Bất kỳ đề xuất nào để sửa lỗi này?

Trả lời

13

Chỉ cần đặt: tags_attributes thay vì: thẻ. Xin vui lòng tham khảo dưới đây . Điều này sẽ giải quyết vấn đề như cùng phải đối mặt bởi tôi

class Post < ActiveRecord::Base 
attr_accessible :title, :content, :tags_attributes 
end 
6

này đã làm việc cho tôi:

class Post < ActiveRecord::Base 
    attr_accessible :name, :title, :content, :tags_attributes 
end 
1

này làm việc cho tôi quá:

class Post < ActiveRecord::Base 
    attr_accessible :title, :content, :tags_attributes 
end 

này cho phép bạn thẻ truy cập các thuộc tính thông qua bài viết .

Các vấn đề liên quan