質問編集履歴

3

変更

2016/11/02 06:37

投稿

maonmyo
maonmyo

スコア36

test CHANGED
File without changes
test CHANGED
@@ -158,18 +158,24 @@
158
158
 
159
159
 
160
160
 
161
+ #パスワードの変更する場合
162
+
161
163
  if params[:password]
162
164
 
163
165
  @edit_user.update_attributes(:password => "#{params[:password]}", :age => "#{params[:age]}")
164
166
 
167
+ render action: :index
168
+
169
+
170
+
171
+ #パスワードの変更しない場合
172
+
173
+ else
174
+
175
+ @edit_user.update_attributes(:age => "#{params[:age]}")
176
+
165
177
  render action: :edit
166
178
 
167
- else
168
-
169
- @edit_user.update_attributes(:age => "#{params[:age]}")
170
-
171
- render action: :edit
172
-
173
179
  end
174
180
 
175
181
  end

2

変更

2016/11/02 06:37

投稿

maonmyo
maonmyo

スコア36

test CHANGED
File without changes
test CHANGED
@@ -1,140 +1,24 @@
1
- チュートリアルとか順序良くやっるうちはあり気にならなかったけど
1
+ 現在、新規登録(new)・更新(edit)機能を作成しす。
2
2
 
3
3
 
4
4
 
5
- ざっくりと画面が作れている状態ののに動作を追加していくことになったら
5
+ 流れはどちらも
6
6
 
7
+ フォーム入力、送信押下、入力チェック、
8
+
9
+ 成功 → index画面へ遷移し、完了メッセージを表示
10
+
11
+ or
12
+
7
- 途端わけがわからくなりまた。
13
+ 失敗 → 同画面てやり
14
+
15
+ です。
8
16
 
9
17
 
10
18
 
11
19
 
12
20
 
13
- 現在、新規登録画面を作成しています。
14
-
15
-
16
-
17
- まずフォーム入力して、送信を押した時に入力チェックが入るようにしています。
18
-
19
- 入力チェックはJavaScriptで書いています。
20
-
21
- IDとパスワードの空欄チェックと、
22
-
23
- パスワードと確認用パスワードの一致です。
24
-
25
-
26
-
27
-
28
-
29
- 現在の動作としては
30
-
31
-
32
-
33
- ・フォーム未入力で送信を押すと、JavaScriptによるalertが表示されますが、延々と出続ける。
34
-
35
-
36
-
37
- ・フォームが全て入力済(パスワードも一致)の状態で送信を押すと、フォームがリセットされる。(URLにパラメータは残ってます)
38
-
39
-
40
-
41
-
42
-
43
- 現在のようになっていま
21
+ ティング下のように設定されていました
44
-
45
-
46
-
47
-
48
-
49
- ```html
50
-
51
-
52
-
53
-
54
-
55
- #divタグ等は省略しています
56
-
57
-
58
-
59
- <form action="/user_mgmts/create" method="post">
60
-
61
- <input type="text" id="user_id" name="user_id">
62
-
63
- <input type="text" id="password" name="password">
64
-
65
- <input type="text" id="password_confirm" name="password_confirm">
66
-
67
- <input type="submit" value="送信" onclick="check()">
68
-
69
-
70
-
71
-
72
-
73
- <script>
74
-
75
- function check() {
76
-
77
- if (!(document.getElementById('user_id').value)) {
78
-
79
- alert('※IDは必須です');}
80
-
81
- if (!(document.getElementById('password').value)) {
82
-
83
- alert('※パスワードは必須です');}
84
-
85
- if ((document.getElementById('password').value) != (document.getElementById('password_confirm').value)){
86
-
87
- alert('※確認用パスワードが一致しません');}
88
-
89
-
90
-
91
- if(check()){
92
-
93
- window.confirm('登録する');
94
-
95
- }else{
96
-
97
- window.alert('修正する');
98
-
99
- }
100
-
101
- }
102
-
103
- </script>
104
-
105
-
106
-
107
- ```
108
-
109
-
110
-
111
- ```Ruby
112
-
113
- [controller]
114
-
115
-
116
-
117
- def new
118
-
119
- @user = User.new
120
-
121
- end
122
-
123
-
124
-
125
- def create
126
-
127
- @user_id = params[:user_id]
128
-
129
- @password = params[:password]
130
-
131
- @user = User.create(:user_id => "#{@user_id}", :password => "#{@password}")
132
-
133
- end
134
-
135
- ```
136
-
137
-
138
22
 
139
23
 
140
24
 
@@ -150,44 +34,162 @@
150
34
 
151
35
 
152
36
 
37
+ #新規登録画面
38
+
39
+ get 'user/new'
40
+
41
+
42
+
153
43
  #更新画面
154
44
 
155
45
  get 'user/edit' => 'user#edit'
156
46
 
47
+ ```
48
+
49
+ ↑このルーティングの設定がよくわかりません。
50
+
51
+ createとupdateアクションを追加したいのですが表記の仕方がわからず。
52
+
53
+
54
+
55
+
157
56
 
158
57
 
159
- #新規登録画面
160
58
 
59
+ ```html
60
+
61
+
62
+
63
+ #divタグ等は省略しています
64
+
65
+
66
+
67
+ <form action="○○○" method="post">
68
+
69
+
70
+
71
+ <input type="text" id="user_id" name="user_id">
72
+
73
+ <input type="text" id="age" name="age">
74
+
75
+ <input type="text" id="password" name="password">
76
+
77
+ <input type="text" id="password_confirm" name="password_confirm">
78
+
79
+
80
+
81
+ <input type="submit" value="送信" onclick="check()">
82
+
83
+
84
+
85
+
86
+
87
+ <script>
88
+
89
+ //ここに入力チェックのコード
90
+
91
+ function check() {
92
+
93
+ ・・・
94
+
95
+ }
96
+
97
+ </script>
98
+
99
+ ```
100
+
101
+ ↑formタグのactionに色々入れてみましたが何も変わりません。
102
+
103
+ method="post"を指定してもpostにならず。
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+ ```Ruby
112
+
113
+ [controller]
114
+
115
+
116
+
117
+ def index
118
+
119
+ #ユーザー検索機能があり、検索ボタン押下でユーザーのデータを表示させるアクション
120
+
121
+ end
122
+
123
+
124
+
125
+ def new
126
+
127
+ @new_user = User.new
128
+
129
+ end
130
+
131
+
132
+
133
+ def create
134
+
135
+ if (@new_user = User.new(:user_id => "#{params[:user_id]}",:age=> "#{params[:age]}",:password => "#{params[:password]}",))
136
+
137
+ @new_user.save
138
+
139
+ render 'index'
140
+
141
+ else
142
+
161
- get 'user/new'
143
+ render 'new'
144
+
145
+ end
146
+
147
+ end
148
+
149
+
150
+
151
+ def edit
152
+
153
+ #index画面で表示されたデータ一覧の、IDをクリックして更新画面へ遷移
154
+
155
+ #IDからデータを検索してフォームに初期値として表示させる
156
+
157
+ @edit_user = User.where("user_id = ?", params[:user_id])
158
+
159
+
160
+
161
+ if params[:password]
162
+
163
+ @edit_user.update_attributes(:password => "#{params[:password]}", :age => "#{params[:age]}")
164
+
165
+ render action: :edit
166
+
167
+ else
168
+
169
+ @edit_user.update_attributes(:age => "#{params[:age]}")
170
+
171
+ render action: :edit
172
+
173
+ end
174
+
175
+ end
162
176
 
163
177
  ```
164
178
 
165
179
 
166
180
 
181
+ createアクションを追記
167
182
 
168
-
169
- ルーティングの仕組みがイマイチ解らなくて、new画面でnewアクションがあり、送信押下でcreateアクションに進められていないのだと思っています。
170
-
171
- routes.rbに
172
-
173
- post 'user' => 'user#create' や、
174
-
175
- post 'user/new' => 'user#create' を入れてみたり。
176
-
177
- リソースでcreateをオリー追加てみたり、
183
+ new画面からcreateアクショへ流すにはどうしたらよいのでしょうか。
178
184
 
179
185
 
180
186
 
181
- HTMLformタグのaction変えみたりmethod="POST"にしてみ
187
+ editアクション中にupdate文記述しいますがupdateアクションを追加し、そこ記述したいと思いますが、
182
188
 
183
- 出来る限り自分でやってみましたがどうにもりませんした
189
+ createと同じくわから
184
190
 
185
191
 
186
192
 
187
193
 
188
194
 
189
-
190
-
191
- 基本的に勉強不足で、ルーティングの仕組みがよく分からないということだけではないのは重々承知ですが、
192
-
193
- ご助力をお願します
195
+ なた教えてください。

1

追記

2016/11/02 06:33

投稿

maonmyo
maonmyo

スコア36

test CHANGED
File without changes
test CHANGED
@@ -188,4 +188,6 @@
188
188
 
189
189
 
190
190
 
191
+ 基本的に勉強不足で、ルーティングの仕組みがよく分からないということだけではないのは重々承知ですが、
192
+
191
- 基本的に勉強不足なのは重々承知ですが、どうかご助力をお願いします。
193
+ どうかご助力をお願いします。