Skip to content

Instantly share code, notes, and snippets.

@raecoo
Created September 10, 2009 08:26
Show Gist options
  • Save raecoo/184413 to your computer and use it in GitHub Desktop.
Save raecoo/184413 to your computer and use it in GitHub Desktop.
# i18n
en:
activerecord:
models:
topic: Topic
attributes:
topic:
posts_body: Body
# topic
class Topic < ActiveRecord::Base
attr_accessor :body
has_many :posts, :autosave => true
validates_presence_of :title
end
# post
class Post < ActiveRecord::Base
belongs_to :topic
validates_presence_of :body
end
# topic_controller/create action
def create
@topic = Topic.new(params[:topic])
@topic.posts << Post.new(:body => params[:topic][:body])
@topic.save ? redirect_to(@topic) : render :action => :new
end
# new.html.erb
<h1>Create new topic</h1>
<%= error_messages_for :topic %>
<% form_for @topic do |f| -%>
<p>
<label>title</label><br/>
<%= f.text_field :title %>
</p>
<p>
<label>body</label><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit 'create' %>
</p>
<% end -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment