LoginSignup
6
4

More than 5 years have passed since last update.

予約機能続き3 借りた人側と貸した人側のviewを作る。 親子routeのview 

Posted at

やりたいこと

前回までに予約機能を作った。ここで、貸した人側と貸した側の viewを作る。

環境

環境
 rails 5.0.2

routesを作る

親子routesを作ると以下になる。
 

routes.rb

 resources :books do
    resources :reservations
  end

 rake routesをすると下になる。

   book_reservations GET    /books/:book_id/reservations(.:format)          reservations#index
                         POST   /books/:book_id/reservations(.:format)          reservations#create
    new_book_reservation GET    /books/:book_id/reservations/new(.:format)      reservations#new
   edit_book_reservation GET    /books/:book_id/reservations/:id/edit(.:format) reservations#edit
        book_reservation GET    /books/:book_id/reservations/:id(.:format)      reservations#show
                         PATCH  /books/:book_id/reservations/:id(.:format)      reservations#update
                         PUT    /books/:book_id/reservations/:id(.:format)      reservations#update
                         DELETE /books/:book_id/reservations/:id(.:format)      reservations#destroy
                   books GET    /books(.:format)                                books#index
                         POST   /books(.:format)                                books#create
                new_book GET    /books/new(.:format)                            books#new
               edit_book GET    /books/:id/edit(.:format)                       books#edit
                    book GET    /books/:id(.:format)                            books#show
                         PATCH  /books/:id(.:format)                            books#update
                         PUT    /books/:id(.:format)                            books#update
                         DELETE /books/:id(.:format)                            books#destroy

借りた人側と貸した人側のviewを作る思考

自分の仮説

  book_reservations GET    /books/:book_id/reservations(.:format)          reservations#index

http://localhost:3000/reservationsとなると、 routesが存在しないと言うエラーが出た。。。
 これは作るしかないと思ってindexを作った。

routesの記述追加

routes.rb
   resources :books do
    resources :reservations
  end

   get '/reservations' => 'reservations#index' 
   get '/lend' => 'reservations#lend'

下の2個を追加した。
 get '/reservations' => 'reservations#index'が予約する人側のview
get '/lend' => 'reservations#lend' が貸し出す人のviewとなる。

コントローラーの追加

reservations.controller.rb
  def index
       @reservations = current_user.reservations.all
   end

   def lend
     @books = current_user.books
   end 

SQliteで見るとこうなる

screenshot.png

def indexの説明

これは予約する側のコントローラーです。どうやって予約した人を取り出すか言うと、user_idから引っ張っていています。だからcurrent_userを入れています。

def lendの説明

これは貸す人側のコントローラーです。 book_idを使って貸す人を特定します。

bookのtableは以下になる

screenshot 2.png

 book_idからbookデーブルを使って、貸す側のuser_idを知ることができる。user_idは、コントローラーに現在のユーザーだけの情報を取り出すために、current_userを使う

だからこうなる
   @books = current_user.books

viewを作る。

app/views/reservations/index.html.erb

<table>
 <% @reservations.each do |reservation| %>


 <%= reservation.book.user.username%>
 <%= reservation.book.title%>
 <%= reservation.start_date.strftime('%Y年%m月%d日') %>
 <%= reservation.end_date.strftime('%Y年%m月%d日') %>



 <% end %>
 </table>


strtimeの説明こちら
http://qiita.com/kitaokeita/items/73eb087583ac3cd4217b

ここシンプルな感じだ。

次が、貸す側のviewだ。

app/views/reservations/lend.html.erb
 <% @books.each do |book| %>
 <% book.reservations.each do |reservation| %>

<%= reservation.user.username %>

<%= reservation.start_date.strftime('%Y年%m月%d日') %>
<%= reservation.end_date.strftime('%Y年%m月%d日') %>


<% end %>

<% end %>

これで完成

感想

 個人的には難しかった。 まだまだ予約機能はできない。たとえば、日付が被ったら、エラーがでるとか

参考資料

 

 

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4