質問編集履歴
1
エラー画面、並びにコントローラーの内容を正しいものに修正しました。何卒よろしくお願いいたします。
title
CHANGED
File without changes
|
body
CHANGED
@@ -75,32 +75,62 @@
|
|
75
75
|
```
|
76
76
|
|
77
77
|
【コントローラー】
|
78
|
-
```app>controllers>articles_controller.rb
|
78
|
+
```ruby:app>controllers>articles_controller.rb
|
79
79
|
class ArticlesController < ApplicationController
|
80
80
|
require 'nokogiri' #import宣言
|
81
81
|
require 'open-uri'
|
82
82
|
|
83
|
+
before_action :move_to_index, except: [:index, :show]
|
83
84
|
|
85
|
+
def index
|
86
|
+
@articles = Article.includes(:user).order("created_at DESC")
|
87
|
+
end
|
88
|
+
|
89
|
+
def new
|
90
|
+
end
|
91
|
+
|
84
92
|
def create
|
85
|
-
url = article_params[:url]
|
93
|
+
url = article_params[:url] #scraping対象URL
|
86
94
|
charset = nil
|
87
95
|
html = crawling(url)
|
88
96
|
|
89
97
|
doc = Nokogiri::HTML.parse(html, nil, charset)
|
90
98
|
title = doc.title
|
91
99
|
image_url = doc.css('img').attribute('src').value
|
92
|
-
|
93
100
|
scrape = insert_scrape(url, title, image_url)
|
94
101
|
article = scrape.articles.build(article_params)
|
95
102
|
end
|
96
103
|
|
104
|
+
def show
|
105
|
+
@article = Article.find(params[:id])
|
106
|
+
@comments = @article.comments.includes(:user)
|
107
|
+
end
|
97
108
|
|
109
|
+
def destroy
|
110
|
+
article = Article.find(params[:id])
|
111
|
+
article.destroy if article.user_id == current_user.id
|
112
|
+
end
|
113
|
+
|
114
|
+
def edit
|
115
|
+
@article = Article.find(params[:id])
|
116
|
+
end
|
117
|
+
|
118
|
+
def update
|
119
|
+
article = Article.find(params[:id])
|
120
|
+
if article.user_id == current_user.id
|
121
|
+
article.update(article_params)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
98
125
|
private
|
99
126
|
|
100
127
|
def article_params
|
101
128
|
params.permit(:url, :title, :text)
|
102
129
|
end
|
103
130
|
|
131
|
+
def move_to_index
|
132
|
+
redirect_to action: :index unless user_signed_in?
|
133
|
+
end
|
104
134
|
|
105
135
|
def insert_scrape(url, title, image_url)
|
106
136
|
scrape = Scrape.new(
|
@@ -119,6 +149,7 @@
|
|
119
149
|
:random_id => random_id
|
120
150
|
)
|
121
151
|
article.save!
|
152
|
+
|
122
153
|
end
|
123
154
|
|
124
155
|
def crawling(url)
|
@@ -127,8 +158,6 @@
|
|
127
158
|
f.read
|
128
159
|
end
|
129
160
|
end
|
130
|
-
|
131
|
-
|
132
161
|
end
|
133
162
|
|
134
163
|
```
|
@@ -136,7 +165,7 @@
|
|
136
165
|
|
137
166
|
### 発生している問題・エラーメッセージ
|
138
167
|
|
139
|
-

|
140
169
|
|
141
170
|
|
142
171
|
### 補足情報
|