[Ruby] Make a family todo list with Sinatra

less than 1 minute read

Introduction

@kirinri posted Lovely article-Qiita, so try to ride it.

Completion screen

20200805.png

Devised points

When you delete a user, delete Todo for that user

main.rb


post'/destroy_user' do
  Todos.where('users_id = ?', params[:users_id]).destroy_all
  Users.find(params[:users_id]).destroy
  redirect to('/')
end

It’s nice because you don’t have to write SQL as “activerecord”.

Round the corners of an item

style.css


.user_btn {
  height: 28px;
  background-color: #FFAD90;
  border-style: none;
  border-radius: 5px;
}

~~ 3 lines in the latter half ~ ~ The process of rounding the last line @scivola-san taught me.

Addition

If you delete a user, it will not be reloaded and the deleted user will remain in the drop-down list. 20200806.png If you add it to the list as that user, an exception will occur. 20200806error.png

main.rb


post'/create_todo' do
  begin
    Todos.create(body: params[:body],users_id: params[:users_id])
  rescue => e
  end
  redirect to('/')
end

I’ve added exception handling, but I think it’s not a compliment, since it’s just passing through.

Summary

  • Thank you @ kirinri
  • Made a todo list with Sinatra
  • ActiveRecord is too convenient

Referred site Create a family todo list with Sinatra