# Problems using Globalize 2 crud with rails 2.3 nested attributes

**URL:** <https://www.ruby-forum.com/t/problems-using-globalize-2-crud-with-rails-2-3-nested-attributes/163254>\
**Category:** Rails\
**Created:** [March 18, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/problems-using-globalize-2-crud-with-rails-2-3-nested-attributes/163254 "2009-03-18T17:43:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [March 18, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/problems-using-globalize-2-crud-with-rails-2-3-nested-attributes/163254/1 "2009-03-18T17:43:18Z")

</div>

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|  
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|
   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|
   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|
   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 |format|
   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 |format|
   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|
   format.html { redirect_to(articles_url) }
   format.xml { head :ok }
 end

```

end  
end

Views: app/views/articles/

index.html.erb

# Listing articles

\<% for article in @articles %\>

\<% end %\>

| id | title |
| \<%= article.id %\> | \<%= article.title %\> | \<%= link\_to 'Show', article %\> | \<%= link\_to 'Edit', edit\_article\_path(article) %\> | \<%= link\_to 'Destroy', article, :confirm =\> 'Are you sure?', :method =\> :delete %\> |

  

\<%= link\_to ‘New article’, new\_article\_path %\>

edit.html.erb

# Editing article

\<% form\_for(@article, :html =\> {:multipart =\> true}) do |f| %\>  
\<%= render :partial =\> “form”, :locals =\> {:f =\> f} %\>

\<%= f.submit "Update" %\>

\<% end %\>

\<%= link\_to ‘Show’, @article %\> |  
\<%= link\_to ‘Back’, articles\_path %\>

new.html.erb

# New article

\<% form\_for(@article, :html =\> {:multipart =\> true}) do |f| %\>  
\<%= render :partial =\> “form”, :locals =\> {:f =\> f} %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, articles\_path %\>

\_form.html.erb

\<% f.fields\_for :globalize\_translations do |trans| %\>

Locale: \<%= trans.text\_field :locale %\>

  

Title: \<%= trans.text\_field :title %\>

  

Text: \<%= trans.text\_area :text %\>

  
  
  
\<% end %\>

show.html.erb

\<%= @article.title %\>

\<%= @article.text %\>

\<%= 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:  
[http://localhost:3000/articles/new](http://localhost:3000/articles/new)  
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…

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [April 2, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/problems-using-globalize-2-crud-with-rails-2-3-nested-attributes/163254/2 "2009-04-02T21:33:52Z")

</div>

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

On 18 Mrz., 19:41, Gianluca T.

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [June 8, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/problems-using-globalize-2-crud-with-rails-2-3-nested-attributes/163254/3 "2010-06-08T17:11:46Z")

</div>

The association name changed from :globalize\_translations to  
:translations. Maybe is this is your problem?
