multiple instances same model same view

154 views
Skip to first unread message

Angelo Cordova

unread,
Jul 21, 2011, 6:07:21 PM7/21/11
to Ruby on Rails: Talk
Hi everyone

I wanna know, if possible, how can I create multiple instances of one
and only one model using the same view.
eg

Model users (with no other associations) and I want to create several
users using the "form" to create users at the same time


New User

name_______________ email__________________ (user 1)

name_______________ email__________________ (user 2)
.
.
.
.

add new user (link or button)


Submit (button)

Curtis Schofield

unread,
Jul 21, 2011, 6:37:16 PM7/21/11
to rubyonra...@googlegroups.com

On Jul 21, 2011, at 3:07 PM, Angelo Cordova wrote:

> Hi everyone
>
> I wanna know, if possible, how can I create multiple instances of one
> and only one model using the same view.
> eg
>

yes - break the problem down into steps IE:

create a new User with

create 2 new Users with one Form

create a new user form field entry (like the 2nd one above) through javascript

create 3 new users + one by clicking the link.

--
Here is an example of a cucumber test for the 2nd part
Given I am on new user page
When I fill in 'sam' for 'user[name]'
When I fill in 's...@example.org' for 'user[email]'
When I press 'Create'
Then I should see 'created (1) user'

--

The second part is a bit more interesting.

---

It helps to know this about FORMS and RAILS
(don't mind the \ )

$curl -d object\[cake\]\[\]=content\&object\[cake\]\[\]=content2 localhost:3000/things

results in
Parameters: {"object"=>{"cake"=>["content", "content2"]}}
--

This concept can be applied to the name of your input property - they can all have the same
name!

--
<input name='user[name][]'></input>
<input name='user[name][]'></input>
<input name='user[name][]'></input>

---
The server will be posted something like :
----
user[name][]=1
user[name][]=3
user[name][]=3
user[name][]=4
user[name][]=54

----
The rails server when it sees this stores a hash in 'params'
with the following structure.

---
=> {user: { name: [ 1,3,3,4,53] }

--


> Model users (with no other associations) and I want to create several
> users using the "form" to create users at the same time
>
>
> New User
>
> name_______________ email__________________ (user 1)
>
> name_______________ email__________________ (user 2)
> ..
> ..

> ..
> ..


>
> add new user (link or button)
>
>
> Submit (button)
>

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>

Reply all
Reply to author
Forward
0 new messages