Skip to content

First commit! #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/todos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def edit
# POST /todos
def create
@todo = Todo.new(todo_params)
@todo.img = params[:todo][:img_blob] if params[:todo][:img_blob]

if @todo.save
redirect_to @todo, notice: 'Todo was successfully created.'
Expand All @@ -32,6 +33,9 @@ def create

# PATCH/PUT /todos/1
def update
# save_image(todo_params)
@todo.img = params[:todo][:img_blob] if params[:todo][:img_blob]

if @todo.update(todo_params)
redirect_to @todo, notice: 'Todo was successfully updated.'
else
Expand All @@ -54,6 +58,7 @@ def set_todo

# Only allow a trusted parameter "white list" through.
def todo_params
params.require(:todo).permit(:title)
params.require(:todo).permit(:title, :img_path)
end

end
11 changes: 11 additions & 0 deletions app/models/todo.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
class Todo < ActiveRecord::Base

PATH="#{Rails.root}/public/todo_imgs"

def img=(img)
ext = File.extname(img.tempfile.path)
path = File.join(PATH, "#{self.id}_img#{ext}")
FileUtils.cp(img.tempfile.path, path)
parts = path.split("/")
path = "/#{parts[parts.length - 2, parts.length - 1].join("/")}"
self.img_path = path
end
end
4 changes: 2 additions & 2 deletions app/views/todos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= simple_form_for @todo do |f| %>
<%= f.input :name %>

<%= f.input :title %>
<%= f.input :img_blob, :as => :file %>
<%= f.submit %>
<% end %>
1 change: 1 addition & 0 deletions app/views/todos/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<%= @todo.title %>
<img src="<%= @todo.img_path %>"/>
5 changes: 5 additions & 0 deletions db/migrate/20150827184912_add_img_path_to_todos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddImgPathToTodos < ActiveRecord::Migration
def change
add_column :todos, :img_path, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150714225535) do
ActiveRecord::Schema.define(version: 20150827184912) do

create_table "todos", force: :cascade do |t|
t.string "title"
t.string "img_path"
end

create_table "users", force: :cascade do |t|
Expand Down
Binary file added public/todo_imgs/1_img.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.