質問編集履歴
1
具体的な今のコードを載せていなかったので、自分の考えとともに編集しました
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
railsで管理者ユーザーが一般ユーザーの荷物を登録する機能を実装したい
|
1
|
+
railsで管理者ユーザーが一般ユーザーの荷物を登録する機能を実装したいです
|
body
CHANGED
@@ -1,1 +1,112 @@
|
|
1
|
+
#現在エラーが出ているファイル
|
2
|
+
baggage_paramsメソッドの記述、newアクションとcreateアクションの記述がbuildを使ったものかもしれないです。
|
3
|
+
```
|
4
|
+
class Admin::BaggagesController < ApplicationController
|
5
|
+
before_action :if_not_admin
|
6
|
+
before_action :set_baggage, only: [:show, :edit, :destroy]
|
7
|
+
def index
|
8
|
+
if current_user.admin?
|
9
|
+
# @baggages = Baggage.search(params[:search])
|
10
|
+
@search = User.ransack(params[:q]) #:qは入力したクエリのq
|
11
|
+
@users = @search.result
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def new
|
17
|
+
if current_user.admin?
|
18
|
+
@baggage = Baggage.new
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def create
|
24
|
+
# @baggage = current_user.baggages.build(baggage_params)
|
25
|
+
# @baggage = Baggage.new.(user_id: @user.id)
|
26
|
+
@baggage = Baggage.new(baggage_params)<--この、行を指してエラーが出ている状態です
|
27
|
+
if @baggage.save
|
28
|
+
|
29
|
+
redirect_to pages_path(@baggage)
|
30
|
+
else
|
31
|
+
render :new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def edit
|
36
|
+
end
|
37
|
+
|
38
|
+
def destroy
|
39
|
+
end
|
40
|
+
|
41
|
+
def show
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
|
49
|
+
def if_not_admin
|
50
|
+
redirect_to root_path unless current_user.admin?#管理者ユーザー以外が特定のアクションを実行しようとした場合トップページにリダイレクトされる
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_baggage
|
54
|
+
@baggage = Baggage.find(params[:id])#edit, show, destroy などのアクションで使用する変数をセットします。
|
55
|
+
end
|
56
|
+
|
57
|
+
def baggage_params
|
58
|
+
params.require(:baggage).permit(
|
59
|
+
:type,:storage_period, :code, ).merge(user_id:[@q])
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
```
|
65
|
+
#遷移元のファイル(admin/baggages/index.html.haml)
|
66
|
+
|
67
|
+
```
|
68
|
+
= search_form_for @search, url: admin_baggages_url do |f|
|
69
|
+
= f.label :name_cont, "名前を入力してください"
|
70
|
+
= f.search_field :name_cont
|
71
|
+
= f.submit "検索"
|
72
|
+
|
73
|
+
.table
|
74
|
+
.thead
|
75
|
+
%tr
|
76
|
+
%th= sort_link(@search, :name)#resultさせてる部分とリンク
|
77
|
+
%th= sort_link(@search, :address)
|
78
|
+
.tbody
|
79
|
+
- @users.each do |user|#controllerの「@users = @search.result」検索したものを"返す"
|
80
|
+
%tr
|
81
|
+
-# %td= link_to user.name, new_admin_baggage_url(@q, :'q[user_id_eq]' => "#{user.id}")
|
82
|
+
%td= user.name
|
83
|
+
%td= user.address
|
84
|
+
%td= link_to "New", new_admin_baggage_url(@q, :'q[user_id_eq]' => "#{user.id}")
|
85
|
+
```
|
86
|
+
#遷移先のファイル(admin/baggages/new.html.haml)
|
87
|
+
= f.hidden_field :user_id, :value => @q(params[:id])の行でuser_idに値が渡せておらず、空で送信されてます
|
88
|
+
```
|
89
|
+
.wrapper
|
90
|
+
|
91
|
+
.disply
|
92
|
+
|
93
|
+
= form_for @baggage ,url: admin_baggages_path do |f|
|
94
|
+
.baggage
|
95
|
+
.baggage__box
|
96
|
+
%span
|
97
|
+
荷物の種類
|
98
|
+
= f.select :type, [["なまもの", "なまもの"], ["チルド", "チルド"], ["冷凍", "冷凍"], ["その他", "その他"]], include_blank: "選択して下さい"
|
99
|
+
.baggage__box
|
100
|
+
%span
|
101
|
+
保管期限
|
102
|
+
= f.text_field :storage_period, class: "baggage_text" , placeholder: '例)7'
|
103
|
+
%span 日
|
104
|
+
.baggage__box
|
105
|
+
%span
|
106
|
+
追跡番号
|
107
|
+
= f.text_field :code, class: "baggage_text", placeholder: '123456789012'
|
108
|
+
= f.hidden_field :user_id, :value => @q(params[:id])<--この部分で値が渡せていません
|
109
|
+
= f.submit 'SEND', class: "baggage__send"
|
110
|
+
|
111
|
+
```
|
1
|
-
|
112
|
+
明らかに実装出来ていない所はコードの横に <-- のマークを付けておきました
|