Jorge Vargas
Jorge Vargas

Reputation: 650

Nested attribute show "Unkown attribute", but it's declared! (Rails 3.2.4)

I have a model called local this model has many portales like this:

class Portal < ActiveRecord::Base
    belongs_to :local, :class_name => "Local"   
end

class Local < ActiveRecord::Base
    has_many :portales, :class_name => "Portal", :dependent => :destroy
    accepts_nested_attributes_for :portales, :allow_destroy => true
end

The problem is when i assign the params to a local, the console shows:

NoMethodError Exception: undefined method `portal' for #<Local:0x007f9cd50a7f20>

Here are my controller and my form:

Controller:

def new_portal
    @local = Local.new
    if request.post?
        @local.update_attributes params[:local]
    end
end

Form:

<%= form_for @local, :url => {:action => 'new_portal'}, :html => {:id => 'formulario_local', :multipart => true, :method => 'post'} do |f| %>
    <%= f.fields_for :portal do |portal_form| -%>
      <div id="porcentaje_barra">
        <%= portal_form.select 'opacidad_barra', generate_options_from_hash(@porcentaje_opacidad) %>
      </div>
      <div id="texto_barra">
        <%= image_tag 'editor/texto.png', :size => '30x30'%>
        <h5>Texto</h5>
      </div>
      <div id="tipo_de_letra_barra">
        <%= portal_form.select 'tipo_letra_barra', generate_options_from_hash(@tipos_de_letra),:prompt => 'Selecciona' %>
      </div>
      <div id="estilo_fuente_barra">
        <%= portal_form.select 'estilo_letra_barra', generate_options_from_hash(@estilo_de_letra), style: 'width:70px'%>
      </div>
      <div id="tam_fuente_barra">
        <%= portal_form.select 'tamano_letra_barra', generate_options_from_hash(@tamano_de_letra), style: 'width:50px'%>
      </div>
    <% end %>
<% end %>

And, here goes my inflections:

ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'usuario', 'usuarios'
inflect.irregular 'producto', 'productos'
inflect.irregular 'portal', 'portales'
inflect.irregular 'local', 'locales'
end

I don't know what is the problem, the view show all the form.

Thanks for your help (and sorry for my English),

Jorge--

Upvotes: 1

Views: 86

Answers (1)

Dmitry Dedov
Dmitry Dedov

Reputation: 1106

First,

<%= f.fields_for :portal do |portal_form| -%>

to

<%= f.fields_for :portales do |portal_form| -%>

Upvotes: 1

Related Questions