質問編集履歴

1

情報の追加

2018/07/10 10:01

投稿

kobashuu
kobashuu

スコア8

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,55 @@
40
40
 
41
41
  // views/books/search
42
42
 
43
+ <div class="row">
44
+
45
+ <div class="col-md-6 col-md-offset-3">
46
+
47
+ <%= form_tag :search, method: :get do %>
48
+
49
+ <%= label_tag :title,'書籍名検索' %>
50
+
51
+ <%= text_field_tag 'title', params[:title] %>
52
+
53
+ <%= submit_tag "検索" %>
54
+
55
+ <% end %>
56
+
57
+ <%= form_tag :search, method: :get do %>
58
+
59
+ <%= label_tag :author,'著者名検索' %>
60
+
61
+ <%= text_field_tag 'author', params[:author] %>
62
+
63
+ <%= submit_tag "検索" %>
64
+
65
+ <% end %>
66
+
67
+ </div>
68
+
69
+ </div>
70
+
71
+
72
+
73
+ <table>
74
+
75
+ <thead>
76
+
77
+ <tr>
78
+
79
+ <th>商品名</th>
80
+
81
+ <th>著者名</th>
82
+
83
+ <th>画像</th>
84
+
85
+ </tr>
86
+
87
+ </thead>
88
+
89
+ <tbody>
90
+
43
- <% if @items.present? %>
91
+ <% if @items.present? %>
44
92
 
45
93
  <% @items.each do |item| %>
46
94
 
@@ -60,6 +108,12 @@
60
108
 
61
109
  <% end %>
62
110
 
111
+ </tbody>
112
+
113
+ </table>
114
+
115
+
116
+
63
117
  ```
64
118
 
65
119
 
@@ -68,44 +122,92 @@
68
122
 
69
123
  // controllers/books.controller.rb
70
124
 
125
+ class BooksController < ApplicationController
126
+
127
+ before_action :logged_in_user, only: [:create, :destroy]
128
+
129
+
130
+
131
+ def search
132
+
133
+ if params[:title] #書籍名で検索
134
+
135
+ @items = RakutenWebService::Books::Book.search(title: params[:title])
136
+
137
+ elsif params[:author] #著者名で検索
138
+
139
+ @items = RakutenWebService::Books::Book.search(author: params[:author])
140
+
141
+ end
142
+
143
+
144
+
145
+ end
146
+
147
+
148
+
149
+
150
+
71
- def create
151
+ def create
72
-
152
+
73
- @book = current_user.books.build(book_params)
153
+ @book = current_user.books.build(book_params)
74
-
154
+
75
- if @book.save
155
+ if @book.save
76
-
156
+
77
- flash[:success] = "本棚に追加しました!"
157
+ flash[:success] = "本棚に追加しました!"
78
-
158
+
79
- redirect_to current_user
159
+ redirect_to current_user
80
-
160
+
81
- else
161
+ else
82
-
162
+
83
- render 'search'
163
+ render 'search'
84
-
164
+
85
- end
165
+ end
166
+
167
+ end
168
+
169
+
170
+
171
+ def destroy
172
+
173
+ end
174
+
175
+
176
+
177
+ private
178
+
179
+
180
+
181
+ def book_params
182
+
183
+ params.require(:book).permit(:title, :author, :user_id)
184
+
185
+ end
86
186
 
87
187
  end
88
188
 
89
189
 
90
190
 
91
-
191
+ ```
92
-
192
+
93
- private
193
+ ```Ruby
194
+
94
-
195
+ // db/schema.rb
196
+
197
+ create_table "books", force: :cascade do |t|
198
+
199
+ t.string "title"
200
+
201
+ t.string "author"
202
+
95
- def book_params
203
+ t.integer "user_id"
204
+
96
-
205
+ t.datetime "created_at", null: false
206
+
97
- params.require(:book).permit(:title, :author, :user_id)
207
+ t.datetime "updated_at", null: false
208
+
98
-
209
+ t.index ["user_id"], name: "index_books_on_user_id"
210
+
99
- end
211
+ end
100
-
212
+
101
- ```
213
+ ```
102
-
103
-
104
-
105
- ### 試したこと
106
-
107
-
108
-
109
-
110
-
111
- ### 補足情報(FW/ツールのバージョンなど)