Skip to content

Commit 8dd5db0

Browse files
author
Matthew Gerrior
committed
First commit!
1 parent 8ac8375 commit 8dd5db0

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

app/controllers/todos_controller.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def edit
2121

2222
# POST /todos
2323
def create
24+
save_image(todo_params)
2425
@todo = Todo.new(todo_params)
2526

2627
if @todo.save
@@ -32,6 +33,13 @@ def create
3233

3334
# PATCH/PUT /todos/1
3435
def update
36+
# save_image(todo_params)
37+
if (todo_params[:img_blob])
38+
img_path = Todo.save_img_blob(todo_params[:img_blob])
39+
params[:img_path] = img_path
40+
params.delete(:img_blob)
41+
end
42+
3543
if @todo.update(todo_params)
3644
redirect_to @todo, notice: 'Todo was successfully updated.'
3745
else
@@ -56,4 +64,12 @@ def set_todo
5664
def todo_params
5765
params.require(:todo).permit(:title)
5866
end
67+
68+
def save_image(todo_params)
69+
if (todo_params[:img_blob])
70+
img_path = Todo.save_img_blob(todo_params[:img_blob])
71+
params[:img_path] = img_path
72+
params.delete(:img_blob)
73+
end
74+
end
5975
end

app/models/todo.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
class Todo < ActiveRecord::Base
2+
3+
PATH='#{Rails.root}/public/todo_imgs'
4+
5+
def self.save_img_blob(blob)
6+
path = File.join(PATH, "#{self.id}_img")
7+
File.open(path, "w") do |file|
8+
file << blob
9+
end
10+
path
11+
end
212
end

app/views/todos/_form.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<%= simple_form_for @todo do |f| %>
2-
<%= f.input :name %>
3-
2+
<%= f.input :title %>
3+
<%= f.input :img_blob, :as => :file %>
44
<%= f.submit %>
55
<% end %>

app/views/todos/show.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<%= @todo.title %>
2+
<img src="<%= @todo.img_path %>"/>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddImgPathToTodos < ActiveRecord::Migration
2+
def change
3+
add_column :todos, :img_path, :string
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20150714225535) do
14+
ActiveRecord::Schema.define(version: 20150827184912) do
1515

1616
create_table "todos", force: :cascade do |t|
1717
t.string "title"
18+
t.string "img_path"
1819
end
1920

2021
create_table "users", force: :cascade do |t|

0 commit comments

Comments
 (0)