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
Then the view that passes "edit_question", @questions into the partial
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' %>
Now, the 'thing' (template, right?) that the partial is passed into:
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 %>
I hope this will be useful to somebody.
Bookmarks