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
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…