diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index 0031b28..64cf404 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -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.' @@ -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 @@ -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 diff --git a/app/models/todo.rb b/app/models/todo.rb index 8886e9b..4ab9e18 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -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 diff --git a/app/views/todos/_form.html.erb b/app/views/todos/_form.html.erb index 27448b3..f9fe5df 100644 --- a/app/views/todos/_form.html.erb +++ b/app/views/todos/_form.html.erb @@ -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 %> diff --git a/app/views/todos/show.html.erb b/app/views/todos/show.html.erb index d3a64d7..1aed6a1 100644 --- a/app/views/todos/show.html.erb +++ b/app/views/todos/show.html.erb @@ -1 +1,2 @@ <%= @todo.title %> + \ No newline at end of file diff --git a/db/migrate/20150827184912_add_img_path_to_todos.rb b/db/migrate/20150827184912_add_img_path_to_todos.rb new file mode 100644 index 0000000..a4cf667 --- /dev/null +++ b/db/migrate/20150827184912_add_img_path_to_todos.rb @@ -0,0 +1,5 @@ +class AddImgPathToTodos < ActiveRecord::Migration + def change + add_column :todos, :img_path, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 481e303..dcab544 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/public/todo_imgs/1_img.jpeg b/public/todo_imgs/1_img.jpeg new file mode 100644 index 0000000..e20e6d4 Binary files /dev/null and b/public/todo_imgs/1_img.jpeg differ