SitePoint Sponsor |
|
User Tag List
Results 1 to 17 of 17
-
Dec 30, 2005, 10:53 #1
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Rails: Multiple edit forms in one view
Hello. thanks in advance for your time, i hope this is a simple question.. (I'm very new (< 1 week) to ruby and rails).
I'd like to create a list view displaying all 'user' records. At the same time, each list item should be a small form with auto-filled fields and a submit button labelled 'update'.
I'm a little stuck on how to implement multiple 'edit' type forms in one view page (all the tutes ive seen deal with only a single form).
My code looks like the listing below. I didn't expect it to work, but incase it's useful to know, at the moment it results in the appropriate number of forms being created, but the fields of the forms aren't being populated:
in the controller:
PHP Code:def list_users
@existingusers= User.find_all
end
def update_user
@existinguser=User.find(@params[:id])
if @existinguser.update_attributes(params[:existinguser])
flash[:notice] = 'Account was successfully updated.'
redirect_to (:action => 'list_users')
else
flash[:error] = 'Account couldnt be updated.'
render :action => 'list_users'
end
end
PHP Code:<%= render(:partial => "user_edit", :collection => @existingusers) %>
PHP Code:<%= form_tag :action=> "update_user", :id => user_edit %>
<label for="user_username">Username:</label><br/>
<%= text_field "user", "username", :size => 15 %><br/>
<label for="user_password">Password:</label><br/>
<%= password_field "user", "password", :size => 15 %><br/>
<label for="user_password_confirmation">Confirm password:</label><br/>
<%= password_field "user", "password_confirmation", :size => 15 %><br/>
<input type="submit" value="Update user" class="primary" />
<%= end_form_tag %>
-
Dec 30, 2005, 11:11 #2
- Join Date
- Nov 2004
- Location
- Yakima WA.
- Posts
- 100
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey bitterclarence-
You need to change the names of your variables inside of the partial. When you do this:
Code:<%= render(:partial => "user_edit", :collection => @existingusers) %>
Code:<%= form_tag :action=> "update_user", :id => existinguser %> <label for="user_username">Username:</label><br/> <%= text_field "existinguser", "username", :size => 15 %><br/> <label for="user_password">Password:</label><br/> <%= password_field "existinguser", "password", :size => 15 %><br/> <label for="user_password_confirmation">Confirm password:</label><br/> <%= password_field "existinguser", "password_confirmation", :size => 15 %><br/> <input type="submit" value="Update user" class="primary" /> <%= end_form_tag %>
Code:<% @existinguser = existinguser %>
Hope that helps.
-Ezra
-
Dec 30, 2005, 12:09 #3
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks for your reply!
i copy pasted your code, and added:
PHP Code:<% @existinguser = existinguser %>
"Showing app/views/account/_user_edit.rhtml where line #1 raised:
undefined local variable or method `existinguser'"
Looking again at the agile web dev book, it looks like it's the :partial parameter that determines the name of the variable you can access inside the partial, not the :collection.. the :collection is the array you pass to the partial..
-
Dec 30, 2005, 13:26 #4
- Join Date
- Nov 2004
- Location
- Yakima WA.
- Posts
- 100
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ahh. I think you are correct. I was just looking at the api and it was a bit unclear which one gets used as the variable inside the partial. Did you get it working?
-
Dec 30, 2005, 13:37 #5
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
no im still a bit stuck..
i need to see an example i think.
At the moment my questions are:
1. How can i create a view with multiple forms that are populated with the data from an array of User.find_all objects?
2. If one of these edit forms is submitted, but the data fails validation defined in the User model, how will the view know which of the edit forms is the source of the bad data? (ie. how would it know which form to apply the error highlighting to?)
-
Dec 30, 2005, 15:23 #6
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
export a hidden field with the id of the object being edited.
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Dec 30, 2005, 16:14 #7
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hi sweatje,
i was thinking that passing the id of the object being edited via a hidden form wouldnt be necessary since the id of each object is already being included in the 'action' path of each form.
in the view:
PHP Code:<%= render(:partial => "user_edit", :collection => @existingusers) %>
PHP Code:<%= form_tag :action=> "update_user", :id => user_edit %>
PHP Code:<form action="/account/update_user/4" method="post">
-
Dec 30, 2005, 16:38 #8
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hi samsm,
the step you described isnt necessary afaik..
i'm using a partial to render each form in the list,
Inside the partial, the current object is referenced by the local variable with the same name as the :partial argument, in my case 'user_edit' (not existinguser)
eg. if i type the following inside the partial:
PHP Code:<%= user_edit.id %></br>
maybe i misunderstood..
-
Dec 30, 2005, 16:46 #9
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by bitterclarence
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Dec 30, 2005, 16:52 #10
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ah! np.. i appreciate you posting
-
Dec 30, 2005, 17:05 #11
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, I'll start over and see if I can actually understand what is going on.
Originally Posted by bitterclarence
In the view:
Code:<%= render(:partial => "user_edit", :collection => @existingusers) %>
Code:<% @existing_user = user_edit %> <%= form('existing_user') %>
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Dec 30, 2005, 17:17 #12
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks samsm, i hadn't seen the 'form('existing_user')' command before.. thats very handy
.. now i have the list of forms being correctly populated at least.
this is a big step forward for me, thanks again!
yeah, the error notification will be tricky i suppose.. in the event of an error a single form page might be a good way like you said.
-
Dec 30, 2005, 20:06 #13
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This comes to mind as a little messy little compromise ...
in _user_edit.rhtml:
Code:<% @existing_user = user_edit %> <% div_id = "edit_user_" + @existing_user.id.to_s %> <div id="<%= div_id %>"> <%= form_remote_tag :html => { :action => url_for(:controller => "user", :action => "no_ajax_update", :id=>@existing_user.id) }, :update => div_id, :url => { :action => "ajax_update", :id => @existing_user.id } %> *** the rest of the form inputs would go here *** <%= end_form_tag %> </div>
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Dec 30, 2005, 21:17 #14
Perhaps there is a specific reason for doing so, but wouldn't the simplest thing to do here be to have an edit action that handles a single record instead of multiple?
-
Dec 31, 2005, 04:23 #15
- Join Date
- Dec 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
samsm: thanks for that, i'm looking through it to try and figure out whats going on!
luke: i agree a single edit form page seems to be the simplest to build. The reason i'd like a list of forms is that the Objects being listed have a very small number of fields ('methods'?) and i'd like to eliminate the extra clicking that opening a seperate edit page would involve (the resulting edit page would also be largely empty, and i'd also like to avoid that for design reasons).
This is an approach that i often take when building publishing systems in php, so of course its one of the first things i'm trying to do in rails
-
Dec 31, 2005, 10:17 #16
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
One thing I often find annoying is to have to only change one record at a time and press submit when I have a multiple line form (that was one of my early PHP habits as well). I instead now tend to go for a "table at a time" editing where all of the rows displayed are processed at once in the action. I recently started a thread where I had models for "Questions" and "Answers". Here is the code I am using to process all of the "Answer" changes at once:
in the view:
Code:<h2>Answers</h2> <%= start_form_tag :action => 'update_answers', :id => @question %> <% if @answers.size > 0 %> <ol style="list-style-type: upper-alpha"> <% for answer in @answers %> <li> <%= hidden_field "answer#{answer.id}", "id", "value" => answer.id %> <%= text_field "answer#{answer.id}", "answer", "size" => 40, "value" => answer.answer %> <% correct = answer.correct ? "1" : "0" %><select id="answer<%= answer.id %>" name="answer<%= answer.id %>[correct]"> <%= options_for_select [["Wrong","0"],["Correct","1"]], correct %> </select></li> <% end %> </ol> <% end %> New: <%= text_field "answernew", "answer", "size" => 40 %> <select id="answernew_correct" name="answernew[correct]"> <%= options_for_select [["Wrong","0"],["Correct","1"]] %></select><br> <%= submit_tag 'Change Question Answers' %> <%= end_form_tag %>
Code:def update_answers @question = Question.find(params[:id])} update_answers = params.keys.reject { |k| m = k.match /^answer(\d+)$/; true if m.nil? } update_answers.each do |a| update = params[a] answer = Answer.find(update[:id]) answer.answer = update[:answer] answer.correct = update[:correct] == "1" ? true : false answer.save flash[:notice] = 'Answers Updated' end answernew = params[:answernew] if answernew['answer'] != "" add = Answer.new add.question_id = @question.id add.answer = answernew[:answer] add.correct = answernew[:correct] == "1" ? true : false add.save flash[:notice] = 'Answer Added' end redirect_to :action => 'edit', :id => @question end
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Feb 7, 2006, 05:30 #17
- Join Date
- Feb 2006
- Posts
- 4
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm very new to Rails too (3rd day), and needed to create a form for editing of all elements.
Using your examples, after some time I managed to create a form that is populated with the existing values to edit all elements, or 'bulk editing' (just to throw in some words for google). The examples here didn't work for me in the first, so I'm posting the code to how I got it to work.
First, the controller code:
Filename: questions_controller.rb
Code:def edit_questions @questions= Question.find_all end def update_question @question = Question.find(@params[:id]) if @question.update_attributes(params[:question]) flash[:notice] = 'Spørgsmål ' + @question.number + ' was successfully updated.' redirect_to (:action => 'edit_questions') else flash[:error] = 'Account couldnt be updated.' render :action => 'list_questions' end end
Filename: edit_questions.rhtml
Code:<h1>Editing all questions</h1> <tr> <th>Gruppe</th> <th>Nr.</th> <th>Spørgsmål</th> <th>Type</th> </tr> <%= render(:partial => "edit_question", :collection => @questions) %> <%= link_to 'Back', :action => 'list' %>
Filename: _edit_question.rhtml
Code:<% @question = edit_question %> <tr> <%= start_form_tag :action => 'update_question', :id => @question %> <td><label for="group">Gruppe</label><br/> <%= text_field 'question', 'group', "size" => 5 %></td> <td><label for="q_number">Nummer</label><br/> <%= text_field 'question', 'number', "size" => 4 %></td> <td><label for="q_text">Spørgsmål</label><br/> <%= text_area 'question', 'text', "rows" => 4, 'cols' => 30 %></td> <td><label for="q_type">Type</label><br/> <%= select 'question', 'q_type', ['Valg', 'Tekst', 'Valg+Tekst'] %></td> <td><input type="submit" value="Update question" class="primary" /></td> </tr> <%= end_form_tag %>
Bookmarks