2012-02-25 11 views
6

estoy tratando de crear etiquetas para mensajes siguiendo las Rails Guide:No se puede asignar en masa los atributos protegidos: 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 

vistas/mensajes /_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 %> 

vistas/etiquetas/_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 %> 

Pero consigo este error cuando intento crear tags:

Can atributos no protegida masa a asignar : tags_attributes Rails.root: /home/alex/rails/r7

Seguimiento de la aplicación | Seguimiento del marco | Traza completa app/controllers/posts_controller.rb: 25: en `crear' Solicitud

Parámetros:

{ "UTF-8"=> "✓", "authenticity_token"=> "FV/qlfZ4Q5yvPY4VIbpFn65hoTAXdEa4fb4I1Ug4ETE =" , "publicar" => {"título" => "publicar número 5", "contenido" => "publicar número 5 publicar número 5 publicar número 5", "atributos_de_tatos" => {"0" => {" nombre "=>" alimentos, bebidas "}}} "comprometerse"=>" Crear mensaje "}

Cualquier sugerencia de solucionar este problema?

Respuesta

13

Simplemente ponga: tags_attributes en lugar de: tags. Consulte más adelante . Esto va a resolver el problema lo mismo enfrentado por mí

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

Esto funcionó para mí:

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

Esto funcionó para mí también:

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

Esto le permiten etiquetas acceder a los atributos a través del poste .

Cuestiones relacionadas