Problems using Globalize 2 crud with rails 2.3 nested attributes


Hi to all !
Seems that none can help me on ruby-i18n group..., I hope someone can help me... :-(
I've try to make a crud sample application using Globalize2 and Rails 2.3.2 nested attributes.
All works great for edit/update operation but on new/create I cannot insert record, seems that when I save :globalize_translations nested attributes there is a ghost record without values and the validations stop the saving process.
Can someone can help me ? (MANY THANKS IN ADVANCE !!!)
Here the example to reproduce the problem:
Model: app/models/article.rb
class Article < ActiveRecord::Base
translates :title, :text
validates_presence_of :title
accepts_nested_attributes_for :globalize_translations
end
Migration: db/migrate/20090207200522_create_articles.rb
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do t.boolean :published
t.timestamps
end
Article.create_translation_table! :title => :string, :text => :text
end
def self.down
Article.drop_translation_table!
drop_table :articles
end
end
Controller: app/controllers/articles_controller.rb (NOTICE THE BUILD ON NEW ACTION...)
class ArticlesController < ApplicationController
# GET /articles
# GET /articles.xml
def index
@articles = Article.find(:all)
respond_to do format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
# GET /articles/1
# GET /articles/1.xml
def show
@article = Article.find(params[:id])
respond_to do format.html # show.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/new
# GET /articles/new.xml
def new
@article = Article.new
@article.globalize_translations.build
respond_to do format.html # new.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
end
# POST /articles
# POST /articles.xml
def create
@article = Article.new(params[:article])
respond_to do if @article.save
flash[:notice] = 'Article was successfully created.'
format.html { redirect_to(@article) }
format.xml { render :xml => @article, :status => :created, :location => @article }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# PUT /articles/1
# PUT /articles/1.xml
def update
@article = Article.find(params[:id])
respond_to do if @article.update_attributes(params[:article])
flash[:notice] = 'Article was successfully updated.'
format.html { redirect_to(@article) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.xml
def destroy
@article = Article.find(params[:id])
@article.destroy
respond_to do format.html { redirect_to(articles_url) }
format.xml { head :ok }
end
end
end
Views: app/views/articles/
index.html.erb
<h1>Listing articles</h1>
<table>
<tr>
<td>id</td>
<td>title</td>
</tr>
<% for article in @articles %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New article', new_article_path %>
edit.html.erb
<h1>Editing article</h1>
<% form_for(@article, :html => {:multipart => true}) do %>
<%= render :partial => "form", :locals => {:f => f} %>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @article %> |
<%= link_to 'Back', articles_path %>
new.html.erb
<h1>New article</h1>
<% form_for(@article, :html => {:multipart => true}) do %>
<%= render :partial => "form", :locals => {:f => f} %>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
_form.html.erb
<% f.fields_for :globalize_translations do %>
<p>Locale: <%= trans.text_field :locale %></p>
<p>Title: <%= trans.text_field :title %></p>
<p>Text: <%= trans.text_area :text %></p>
<br/>
<% end %>
show.html.erb
<p><%= @article.title %></p>
<p><%= @article.text %></p>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
Routing map: config/routes.rb
map.resources :articles
rake db:migrate
script/server
Then try to insert a new article using: Locale: en
Title: Test title
Text: Test text
When try to create the record nothing happens, but if I create the record in console:
Article.create(:title => "Test title", :text => "Test text")
the edit and update operations works well...
I don't understand why I can't create new records with nested attributes using the view...

Hi,

have you solved this now? Your idea of using the globalize
translations as a nested attribute
seems interesting. I was just wondering why you don't just let
globalize handle the globalization
stuff transparently in the background. You can then simply create
events for different locales
by switching to the locale you want before creating the event. I do it
that way, storing the locale
as a hidden field in the form.

Best Regards,
Nico

The association name changed from :globalize_translations to
:translations. Maybe is this is your problem?