I’m very new to Rails. I’ve been reading Agile Web Dev for a while and
read
up on ruby, but I’m still learning.
I designed a website for someone for an engineering shop. New projects
had
to be added manually.
Well, I figured this would be a great canidate for railifying! I have
the
form working perfectly, you can add your data and get it in a table, and
listing projects works, too. I’m working on a photo gallery now, and I
have
a lot of it working. I took it from the agile web dev book. I’d like to
know
how to join the two forms and submit it as one.
I’m not sure how to submit data to two different places via one form.
Suggestions and links are greatly appreciated. Thanks!
Am Freitag, den 03.03.2006, 21:57 -0500 schrieb Matt R.:
book. I’d like to know how to join the two forms and submit it as
one.
I’m not sure how to submit data to two different places via one form.
Suggestions and links are greatly appreciated. Thanks!
Take two text fields for example:
view
<%= form_tag :controller => :posts, :action => create %>
<%= text_field(“post”, “title”, “size” => 20) %>
<%= text_field(“user”, “email”, “size” => 20) %>
<%= end_form_tag %>
posts_controller
class PostsController < ApplicationController
def create
@post = Post.create(params[:post])
@user = User.create(params[:user])
@post.users << @user
end
end
In the controller the params hash has one key for every object (user,
post) in your form. As a values of this key you find a hash having a
key-value pair for every object attribute (title, email).
This way you can submit multiple objects via one form.
Norman T.
http://blog.inlet-media.de
Norman - wouldn’t you also have to call a “@post.save” at the end to
save the relationship?
Thanks a lot, I got it working.
Am Sonntag, den 05.03.2006, 07:05 -0600 schrieb Michael G.:
end
end
It depends on the kind of association type binding the two models.
If User belongs_to :post and Post has_many :users, the << method will
call save on the child object (User) to make the association permanent.
The same happens to child objects at has_and_belongs_to_many and has_one
associations. So @post.user = @user also needs no save on @user.
In the difference (assume User belongs_to :post as above), if you do
@user.post = @post, you have to do a @user.save after associating them.
Norman T.
http://blog.inlet-media.de