Skip to content

Commit

Permalink
Add edit inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
rociopaez committed Sep 18, 2014
1 parent cd52120 commit 5dcf6e7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions routes/inputs.rb
Expand Up @@ -4,6 +4,23 @@ class Inputs < Cuba
render("inputs/new", title: "New Input", types: Type.all)
end


on(":id") do |id|
input = Input[id]

on post, param("input") do |params|
date = Date.new(params["date_year"].to_i, params["date_month"].to_i, params["date_day"].to_i)
type = ::Type[params["type_id"]]
input = input.update(date: date, type: type)

res.redirect("/inputs")
end

on get do
render("inputs/edit", title: "Edit Input", input: input, user: current_user, types: current_user.types)
end
end

on post, param("input") do |params|
date = Date.new(params["date_year"].to_i, params["date_month"].to_i, params["date_day"].to_i)
type = ::Type[params["type_id"]]
Expand Down
37 changes: 37 additions & 0 deletions views/inputs/edit.mote
@@ -0,0 +1,37 @@
<h2>Edit Inputs</h2>

<form action="/inputs/{{input.id}}" method="POST">
<fieldset>
<label for="date">Date</label>
<select name="input[date_month]">
% 1.upto(12).each do |i|
<option{{ ' selected="selected"' if input.date.month == i }}>{{ i }}</option>
% end
</select>
<select name="input[date_day]">
% 1.upto(31).each do |i|
<option{{ ' selected="selected"' if input.date.day == i }}>{{ i }}</option>
% end
</select>
<select name="input[date_year]">
% this_year = Time.now.year
% start_year = this_year - 2
% start_year.upto(this_year).each do |i|
<option{{ ' selected="selected"' if input.date.year == i }}>{{ i }}</option>
% end
</select>

<label for="types">Types</label>
<select name="input[type_id]">
% params[:types].group_by { |type| type.category }.each do |category, types|
<optgroup label="{{ category }}">
% types.each do |type|
<option value="{{ type.id }}">{{ type.name }}</option>
% end
</optgroup>
% end
</select>

</fieldset>
<input type="submit" value="send">
</form>
5 changes: 5 additions & 0 deletions views/inputs/index.mote
Expand Up @@ -9,6 +9,7 @@
<th>Date</th>
<th>Category</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
Expand All @@ -17,7 +18,11 @@
<td>{{input.date}}</td>
<td>{{input.type.category}}</td>
<td>{{input.type.name}}</td>
<td><a href="/inputs/{{input.id}}">Edit</a></td>
</tr>
% end
</tbody>
</table>


<!-- <td class="action"><a href="/types/edit">Edit</a> - <a href="###">Delete</a></td> -->

0 comments on commit 5dcf6e7

Please sign in to comment.