Forum: Rails Germany Mal wieder 'ne Frage: accepts_nested_attributes

Posted by Michael Kastner (Guest)
on 2009-04-29 19:26
(Received via mailing list)
Hi,

ich stecke hier gerade fest und weiß nicht warum. Der Sachverhalt

Drei Klassen

Publication Category Topic

Publication und Topic sind verbunden über Category via has_many through.

Publication sieht so aus:


class Publication < ActiveRecord::Base

   validates_presence_of :name

   has_many :topics, :through => :categories
   has_many :categories

   accepts_nested_attributes_for :topics

end

Problem ist, wenn ich eine neue Publication erstellen will, erhalte ich 
eine
Fehlermeldung:

Topic(#19862330) expected, got HashWithIndifferentAccess(#12583160)
...

Die Parameter werden korrekt übergeben:

{"commit"=>"Speichern",
  "publication"=>{"name"=>"wqer",
  "topics"=>[{"name"=>""},
  {"name"=>""},
  {"name"=>""}]}}


Hat jemand eine Idee, woran es hängen könnte?

Viele 
Grüße
Michael Kastner
Posted by Manuel Wiedenmann (Guest)
on 2009-04-29 20:31
(Received via mailing list)
Wie sieht denn dein Formular aus?

Hatte genau den selben Fehler und da lag es glaub ich daran, dass ich
ein Symbol statt einer Instanzvariable oder umgekehrt genutzt habe...

so sieht es z.b. bei mir aus:

class UsersController < ApplicationController

   # render new.rhtml
   def new
     @user = User.new
     @user.addresses.build
   end

end

###################################

class User < ActiveRecord::Base
   has_many  :addresses,
             :dependent    => :destroy

   attr_accessible :addresses_attributes //Wichtig

   accepts_nested_attributes_for :addresses

end

###################################

<% form_for @user do |user_form| -%>
<%= user_form.error_messages %>

...

  <h1><%= t("user.address.title") %></h1>

  <% user_form.fields_for :addresses do |address_form| %>

    <%= render :partial => "addresses/form_fields", :locals => {:f =>
address_form, :email => nil} %>

  <% end %>

  <p><%= submit_tag t('forms.buttons.sign_up') %></p>
<% end -%>

Gruß
Manuel


Am 29.04.2009 um 19:25 schrieb Michael Kastner:
Posted by Michael Kastner (Guest)
on 2009-04-30 07:40
(Received via mailing list)
Hallo Manuel,

ich habe mein Formular unten angehängt. Die Zeile

attr_accessible :addresses_attributes

in Deinem Model User ist doch eigentlich redundant. Das sollte doch, 
wenn ich es
richtig verstanden habe, schon im Zuge von

accepts_nested_attributes_for :addresses

bereits vorhanden sein, oder nicht?

Wenn ich bei mir im model

attr_accessible :topics_attributes

verwende, dann erhalte ich im log die Meldung:

WARNING: Can't mass-assign these protected attributes: name, topics


Hier mein Formular-Code:

new.html.erb:

<h1>Neue Publikation erstellen</h1>
<% form_for :publication,
             @publication,
             :url => { :action => 'create' } do |f| %>
<%= render :partial => 'form', :locals => {:f => f} %>
<% end %>

_form.html.erb:

<%= error_messages_for :publication %>

<%= f.text_field 'name' %>
<p>Themen</p>
<% @publication.topics.each do |topic| %>
   <% fields_for 'publication[topics][]', topic do |topic| %>
     <%= topic.text_field :name %>
   <% end %>
<% end %>
<%= submit_tag 'Speichern' %>


Eigentlich müßte es funktionieren.

Viele 
Grüße
Michael Kastner


Manuel Wiedenmann schrieb:
Posted by Michael Kastner (Guest)
on 2009-04-30 08:13
(Received via mailing list)
Hi Manuel,

ich habe die Ursache für mein Problem gefunden.

Es lag an den Benennung der Felder im Formular:

<% @publication.topics.each do |topic| %>
   <% fields_for 'publication[topics][]', topic do |topic| %>
     <%= topic.text_field :name %>
   <% end %>
<% end %>

statt

'publication[topics][]'

muß es

'publication[topics_attributes][]'

heißen.

Anyway, vielen Dank für Deine Hilfe.

Michael Kastner

Manuel Wiedenmann schrieb:
Posted by Manuel Wiedenmann (Guest)
on 2009-04-30 08:26
(Received via mailing list)
könntest du das nicht kürzen:

  <% f.fields_for : topics do |topic_form| %>
    <%= topic_form.text_field :name %>
  <% end %>

somit sollte das each nicht mehr nötig sein...


Am 30.04.2009 um 08:12 schrieb Michael Kastner:
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.