Assocations between models

224 views
Skip to first unread message

mxrgus pxrt

unread,
Apr 21, 2010, 1:22:48 PM4/21/10
to Padrino Framework
Post
- id
- title
- text

Comment
- post_id
- id
- text

I have noted belongs_to and has_many relations in ar models.

padrino-gen admin_page post
padrino-gen admin_page comment


If I add in admin new comment, its possible to write ID of post, but
not possible to select it from drop down box - also when opening post
the comments are not shown.

Is or will there be any functionality what automatically generate this
code?

I should modify controllers and views? What is the prefered way to do
this?


--
Subscription settings: http://groups.google.com/group/padrino/subscribe?hl=en

DAddYE

unread,
Apr 21, 2010, 6:07:28 PM4/21/10
to Padrino Framework
Yep, now admin didn't create for you the right "controller" for your
association, this simply because it's very tricky do that for all our
adapters, each adapter have their own relationships.

Btw with ar is super simpler "hack" this in you comment form change
this:

=f.text_field :post_id

in

=f.select :post_id, :collection => Post.all

That's all!

mxrgus pxrt

unread,
May 13, 2010, 5:46:56 AM5/13/10
to Padrino Framework
Thank you for your response, but this does not do the trick:

padrino-gen project sample_blog -t shoulda -e haml -c sass -s jquery -
d activerecord -b
cd sample_blog
padrino-gen admin
padrino rake ar:create
padrino rake ar:migrate
padrino rake seed

padrino-gen model post title:string body:text
padrino-gen model comment post_id:integer body:text
padrino rake ar:migrate

padrino-gen admin_page post
padrino-gen admin_page comment

# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end

#app/models/post.rb
class Post < ActiveRecord::Base
has_many :comments
end

-#admin/views/comments/_form.haml
.group
=f.label :post_id
=f.error_message_on :post_id
-# =f.text_field :post_id, :class => :text_field
=f.select :post_id, :collection => Post.all
%span.description Ex: a simple text

.group
=f.label :body
=f.error_message_on :body
=f.text_area :body, :class => :text_area
%span.description Ex: a simple text


.group.navform.wat-cf
=f.submit pat(:save), :class => :button
=f.submit pat(:cancel), :onclick =>
"window.location='#{url(:comments, :index)}';return false", :class
=> :button






=f.select :post_id, :collection => Post.all

Davide D'Agostino

unread,
May 13, 2010, 5:51:57 AM5/13/10
to pad...@googlegroups.com
Yep sorry I forgot that is necessary fields:

=f.select :post_id, :collection => Post.all, :fields => [:name, :id]

where :name is the db filed that you want show.

More info here: http://www.padrinorb.com/guides/application-helpers#formbuilders

mxrgus pxrt

unread,
May 22, 2010, 6:04:06 PM5/22/10
to Padrino Framework
Works like a charm, thank you for your response. I'll continue
learning your framework now. :)

On May 13, 12:51 pm, Davide D'Agostino <d.dagost...@lipsiasoft.com>
wrote:

mxrgus pxrt

unread,
May 22, 2010, 7:18:27 PM5/22/10
to Padrino Framework
Could you please add also an example how to add all given comments to
some post in admin/views/posts/_form.haml
Reply all
Reply to author
Forward
0 new messages