Skip to content

Commit

Permalink
Add New Input and save by user
Browse files Browse the repository at this point in the history
  • Loading branch information
rociopaez committed Sep 12, 2014
1 parent 5da5239 commit 0cfacd5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
12 changes: 11 additions & 1 deletion routes/inputs.rb
Expand Up @@ -4,8 +4,18 @@ class Inputs < Cuba
render("inputs/new", title: "New Input", types: Type.all)
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"]]
input = Input.create(date: date, type: type, user: current_user)

res.redirect("/inputs")
end

on get do
render("inputs/index", title: "Inputs", inputs: Input.all)
render("inputs/index", title: "Inputs", inputs: current_user.inputs) #esto se pasa al index
end


end
end
28 changes: 23 additions & 5 deletions views/inputs/index.mote
@@ -1,7 +1,25 @@
% inputs = params[:inputs]

<h2><a href="/inputs/new">New Input</a></h2>

<ul>
% params[:inputs].each do |inputs|
<li><a href="/inputs/{{ input.id }}">{{ input.type.category }} - {{ input.type.name }}</a></li>
% end
</ul>


<h2>Your Input Types record</h2>
<table>
<thead>
<tr>
<th>Date</th>
<th>Category</th>
<th>Name</th>
</tr>
</thead>
<tbody>
% inputs.each do |input|
<tr>
<td>{{input.date}}</td>
<td>{{input.type.category}}</td>
<td>{{input.type.name}}</td>
</tr>
% end
</tbody>
</table>
11 changes: 5 additions & 6 deletions views/inputs/new.mote
@@ -1,27 +1,27 @@
<h2>Migraine tracker - Inputs</h2>

<form action="/inputs" method="GET">
<form action="/inputs" method="POST">
<fieldset>
<label for="date">Date</label>
<select>
<select name="input[date_month]">
% 1.upto(12).each do |i|
<option{{ ' selected="selected"' if Time.now.month == i }}>{{ i }}</option>
% end
</select>
<select>
<select name="input[date_day]">
% 1.upto(31).each do |i|
<option{{ ' selected="selected"' if Time.now.day == i }}>{{ i }}</option>
% end
</select>
<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 Time.now.year == i }}>{{ i }}</option>
% end
</select>
<label for="types">Types</label>
<select name="input[type]">
<select name="input[type_id]">
% params[:types].group_by { |type| type.category }.each do |category, types|
<optgroup label="{{ category }}">
% types.each do |type|
Expand All @@ -31,6 +31,5 @@
% end
</select>
</fieldset>

<input type="submit" value="send">
</form>

0 comments on commit 0cfacd5

Please sign in to comment.