質問編集履歴
1
情報が不足しており、失礼しました。モデルの情報も追加致しました。
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -8,19 +8,86 @@
|
|
|
8
8
|
|
|
9
9
|
■View
|
|
10
10
|
```rails
|
|
11
|
+
**Show**
|
|
12
|
+
<p>
|
|
13
|
+
<strong>Content2:</strong>
|
|
14
|
+
<%# @campaign.content2.try(:) %>
|
|
15
|
+
<%# @campaign.content2 %>
|
|
16
|
+
<%= text_field_tag 'campaign[content2][]' %>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
**new**
|
|
20
|
+
<%= form_for(@campaign) do |f| %>
|
|
11
21
|
<div class="field">
|
|
22
|
+
<%= f.label :title %><br>
|
|
23
|
+
<%= f.text_field :title %>
|
|
24
|
+
</div>
|
|
25
|
+
<div class="field">
|
|
26
|
+
<%= f.label :content1 %><br>
|
|
27
|
+
<%= f.text_field :content1 %>
|
|
28
|
+
</div>
|
|
29
|
+
<div class="field">
|
|
12
30
|
<%= f.label :content2 %><br>
|
|
13
31
|
<%= f.text_field :content2 ,{:name => name="campaign[content2][]" ,:multipart => true} %>
|
|
14
32
|
<%= f.text_field :content2 ,{:name => name="campaign[content2][]" ,:multipart => true} %>
|
|
15
33
|
</div>
|
|
34
|
+
<div class="actions">
|
|
35
|
+
<%= f.submit %>
|
|
36
|
+
</div>
|
|
37
|
+
<% end %>
|
|
16
38
|
```
|
|
17
39
|
■Controller
|
|
18
40
|
```ここに言語を入力
|
|
41
|
+
**create**
|
|
42
|
+
def create
|
|
43
|
+
@campaign = Campaign.new(campaign_params)
|
|
44
|
+
|
|
45
|
+
respond_to do |format|
|
|
46
|
+
if @campaign.save
|
|
47
|
+
format.html { redirect_to @campaign, notice: 'Campaign was successfully created.' }
|
|
48
|
+
format.json { render :show, status: :created, location: @campaign }
|
|
49
|
+
else
|
|
50
|
+
format.html { render :new }
|
|
51
|
+
format.json { render json: @campaign.errors, status: :unprocessable_entity }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
**Update**
|
|
57
|
+
def update
|
|
58
|
+
respond_to do |format|
|
|
59
|
+
if @campaign.update(campaign_params)
|
|
60
|
+
format.html { redirect_to @campaign, notice: 'Campaign was successfully updated.' }
|
|
61
|
+
format.json { render :show, status: :ok, location: @campaign }
|
|
62
|
+
else
|
|
63
|
+
format.html { render :edit }
|
|
64
|
+
format.json { render json: @campaign.errors, status: :unprocessable_entity }
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
**strong parameter**
|
|
19
70
|
def campaign_params
|
|
20
71
|
params.require(:campaign).permit(:title, :content1, :content2 => [])
|
|
21
72
|
end
|
|
22
73
|
```
|
|
23
74
|
|
|
75
|
+
■model
|
|
76
|
+
```
|
|
77
|
+
**db**
|
|
78
|
+
create_table "campaigns", force: :cascade do |t|
|
|
79
|
+
t.string "title"
|
|
80
|
+
t.string "content1"
|
|
81
|
+
t.string "content2"
|
|
82
|
+
t.datetime "created_at", null: false
|
|
83
|
+
t.datetime "updated_at", null: false
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
**model**
|
|
87
|
+
class Campaign < ActiveRecord::Base
|
|
88
|
+
end
|
|
89
|
+
```
|
|
90
|
+
|
|
24
91
|
そして、入力すると、下記のようにContent2には、「”test","test"]となって、配列で受け取らたように一瞬思いますが、実は配列ではなく文字列でした。
|
|
25
92
|
( @campaign.content2として表示しています。そのためgsub等で、”や[等を削除する必要が出てきてしまいます。)
|
|
26
93
|
|