質問編集履歴

3

文法の修正

2016/01/07 23:54

投稿

probaseball
probaseball

スコア113

test CHANGED
@@ -1 +1 @@
1
- Railsで○○.rbを読み取る場合にやるべきこと
1
+ railsでの処理書いたファイルを読み取りかた
test CHANGED
@@ -1,34 +1,32 @@
1
- 先日ここで教えてもらった[https://www.imd-net.com/column/3534/](https://www.imd-net.com/column/3534/)を参考にしながらプログラムを組んでいましが、わからなところがあったの質問させていただきま
1
+ railsの実際の処理.rb(以下:index.rb)を読み取りたい場合どうすればよいですか?
2
2
 
3
3
 
4
4
 
5
- 上記のサイトで登場する[user.rb]と[routes.rb(中身)]、[user_controllerのAdmin::Users]の所です。
5
+ 参考にした処理 [https://www.imd-net.com/column/3534/](https://www.imd-net.com/column/3534/)
6
-
7
-
8
-
9
- これらはどのような動きをしてるのか教えてください。
10
-
11
-
12
-
13
- 一様ソースを記載しておきます。よろしくお願いします
14
6
 
15
7
 
16
8
 
17
9
  ```ruby
18
10
 
19
- #/config/routes.rb
11
+ class HomeController < ApplicationController
20
12
 
21
- namespace :admin do
22
13
 
23
- resources :users do
24
14
 
25
- collection do
15
+ def index
26
16
 
27
- post :import
17
+
28
18
 
19
+ if params[:button]
20
+
29
- end
21
+ #index.rbを読み取る
30
22
 
31
23
  end
24
+
25
+
26
+
27
+ end
28
+
29
+
32
30
 
33
31
  end
34
32
 
@@ -38,23 +36,17 @@
38
36
 
39
37
  ```ruby
40
38
 
41
- #/app/controllers/admin/users_controller.rb
39
+ <%= form_tag(index_path,multipart: true)do %>
42
40
 
43
- def import
41
+ <div class="form-group">
44
42
 
45
- if params[:csv_file].blank?
43
+ <%= tag :input,{:type =>'submit',:name=>'button',:value =>'ボタンを押してください'} %><br>
46
44
 
47
- redirect_to(admin_users_url, alert: 'インポートするCSVファイルを選択してください')
45
+ </div>
48
46
 
49
- else
47
+ <%= submit_tag"リフレッシュ" %>
50
48
 
51
- num = Admin::User.import(params[:csv_file]) #<-ココ
52
-
53
- redirect_to(admin_users_url, notice: "#{num.to_s}件のユーザー情報を追加 / 更新しました")
54
-
55
- end
49
+ <% end %>
56
-
57
- end
58
50
 
59
51
  ```
60
52
 
@@ -62,82 +54,16 @@
62
54
 
63
55
  ```ruby
64
56
 
65
- #/app/models/admin/user.rb
66
-
67
- # CSVファイルの内容をDBに登録する
68
-
69
- def self.import(file)
57
+ Rails.application.routes.draw do
70
58
 
71
59
 
72
60
 
73
- imported_num = 0
61
+ get '/index' => 'home#index'
74
62
 
75
63
 
76
64
 
77
- # 文字コード変換のためにKernel#openとCSV#newを併用。
78
-
79
- # 参考: http://qiita.com/labocho/items/8559576b71642b79df67
80
-
81
- open(file.path, 'r:cp932:utf-8', undef: :replace) do |f|
82
-
83
- csv = CSV.new(f, :headers => :first_row)
84
-
85
- csv.each do |row|
65
+ end
86
-
87
- next if row.header_row?
88
66
 
89
67
 
90
68
 
91
- # CSVの行情報をHASHに変換
92
-
93
- table = Hash[[row.headers, row.fields].transpose]
94
-
95
-
96
-
97
- # 登録済みユーザー情報取得。
98
-
99
- # 登録されてなければ作成
100
-
101
- user = find_by(:id => table["id"])
102
-
103
- if user.nil?
104
-
105
- user = new
106
-
107
- end
108
-
109
-
110
-
111
- # ユーザー情報更新
112
-
113
- user.attributes = table.to_hash.slice(
114
-
115
- *table.to_hash.except(:id, :created_at, :updated_at).keys)
116
-
117
-
118
-
119
- # バリデーションOKの場合は保存
120
-
121
- if user.valid?
122
-
123
- user.save!
124
-
125
- imported_num += 1
126
-
127
- end
128
-
129
- end
130
-
131
- end
132
-
133
-
134
-
135
- # 更新件数を返却
136
-
137
- imported_num
138
-
139
-
140
-
141
- end
142
-
143
69
  ```

2

文法の修正

2016/01/07 23:54

投稿

probaseball
probaseball

スコア113

test CHANGED
File without changes
test CHANGED
File without changes

1

文法の修正

2016/01/07 10:45

投稿

probaseball
probaseball

スコア113

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,137 @@
7
7
 
8
8
 
9
9
  これらはどのような動きをしてるのか教えてください。
10
+
11
+
12
+
13
+ 一様ソースを記載しておきます。よろしくお願いします
14
+
15
+
16
+
17
+ ```ruby
18
+
19
+ #/config/routes.rb
20
+
21
+ namespace :admin do
22
+
23
+ resources :users do
24
+
25
+ collection do
26
+
27
+ post :import
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ ```
36
+
37
+
38
+
39
+ ```ruby
40
+
41
+ #/app/controllers/admin/users_controller.rb
42
+
43
+ def import
44
+
45
+ if params[:csv_file].blank?
46
+
47
+ redirect_to(admin_users_url, alert: 'インポートするCSVファイルを選択してください')
48
+
49
+ else
50
+
51
+ num = Admin::User.import(params[:csv_file]) #<-ココ
52
+
53
+ redirect_to(admin_users_url, notice: "#{num.to_s}件のユーザー情報を追加 / 更新しました")
54
+
55
+ end
56
+
57
+ end
58
+
59
+ ```
60
+
61
+
62
+
63
+ ```ruby
64
+
65
+ #/app/models/admin/user.rb
66
+
67
+ # CSVファイルの内容をDBに登録する
68
+
69
+ def self.import(file)
70
+
71
+
72
+
73
+ imported_num = 0
74
+
75
+
76
+
77
+ # 文字コード変換のためにKernel#openとCSV#newを併用。
78
+
79
+ # 参考: http://qiita.com/labocho/items/8559576b71642b79df67
80
+
81
+ open(file.path, 'r:cp932:utf-8', undef: :replace) do |f|
82
+
83
+ csv = CSV.new(f, :headers => :first_row)
84
+
85
+ csv.each do |row|
86
+
87
+ next if row.header_row?
88
+
89
+
90
+
91
+ # CSVの行情報をHASHに変換
92
+
93
+ table = Hash[[row.headers, row.fields].transpose]
94
+
95
+
96
+
97
+ # 登録済みユーザー情報取得。
98
+
99
+ # 登録されてなければ作成
100
+
101
+ user = find_by(:id => table["id"])
102
+
103
+ if user.nil?
104
+
105
+ user = new
106
+
107
+ end
108
+
109
+
110
+
111
+ # ユーザー情報更新
112
+
113
+ user.attributes = table.to_hash.slice(
114
+
115
+ *table.to_hash.except(:id, :created_at, :updated_at).keys)
116
+
117
+
118
+
119
+ # バリデーションOKの場合は保存
120
+
121
+ if user.valid?
122
+
123
+ user.save!
124
+
125
+ imported_num += 1
126
+
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+
133
+
134
+
135
+ # 更新件数を返却
136
+
137
+ imported_num
138
+
139
+
140
+
141
+ end
142
+
143
+ ```