Ruby on Rails form_for 创建空对象
Ruby on Rails form_for creates empty object
我的 alerts/views/ 文件夹中有一个名为 _form.html.erb 的部分。我从我的 agencies/views/ 文件夹中的显示页面呈现部分内容。
一切都正确呈现,部分确实创建了一个新警报,但警报完全是空的。
alerts_controller 创建方法
def create
@alert = Alert.new(body: params[:body],
severity: params[:severity],
agency_id: @current_member.id)
respond_to do |format|
if @alert.save
format.html { redirect_to @alert.agency, notice: 'Alert was successfully created.' }
format.json { render :show, status: :created, location: @alert}
else
format.html { render :new }
format.json { render json: @alert.errors, status: :unprocessable_entity }
end
end
end
_form.html.erb
<%= form_for(Alert.new) do |f| %>
<div class="input-group">
<div class="field">
<%= f.label :body %><br>
<%= f.text_field :body %>
</div>
<div class="field">
<%= f.label :severity %><br>
<%= f.number_field :severity %>
</div>
<div class="action">
<%= f.submit %>
</div>
</div>
<% end %>
没有错误,只是创建的警报完全是空的。主体、严重性和 agency_id 变量均为零。
我试过更换线路
<%= form_for(Alert.new) do |f| %>
有了这个:
<%= form_for(@alert) do |f| %>
并添加此行:
@alert = Alert.new
代理控制器中的显示方法。
但是无论哪种方式都会发生同样的事情。我做错了什么?
编辑
这是我点击提交时开始的日志,在 alerts.create 方法中加载重定向之前结束。
Started POST "/alerts" for ::1 at 2016-03-31 18:29:43 -0400
Processing by AlertsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CgpepnYUec9uUoE/Ys6SAkOlzmK+w2q9IN782sXNoXnB3UyuegiS6m+W+mW+nXu4EIL8P8xH6JdigU8FfmzhVw==", "alert"=>{"body"=>"This is the body text", "severity"=>"12345"}, "commit"=>"Create Alert"}
Account Load (0.1ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = ? LIMIT 1 [["id", 7]]
Agency Load (0.1ms) SELECT "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1 [["id", 2]]
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "alerts" ("agency_id", "created_at", "updated_at") VALUES (?, ?, ?) [["agency_id", 2], ["created_at", "2016-03-31 22:29:43.014846"], ["updated_at", "2016-03-31 22:29:43.014846"]]
(135.8ms) commit transaction
Agency Load (0.1ms) SELECT "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1 [["id", 2]]
Redirected to http://localhost:3000/agencies/2
Completed 302 Found in 141ms (ActiveRecord: 136.2ms)
当我注释掉 alerts.create 方法并添加这一行时:
render plain: params
这是输出:
{"utf8"=>"✓",
"authenticity_token"=>"3OfteFX41SV/5NxpTcKbP7AKhLm/ZKah+NXVn84e2xwXMP9wWeQ+AH4gpzORkXKF4y225M3gJIu6imZAdb+bMg==",
"alert"=>{"body"=>"This is the body text.", "severity"=>"12345"},
"commit"=>"Create Alert", "controller"=>"alerts", "action"=>"create"}
从您的 params
调试中可以明显看出根本原因。 params
散列中有一个 alert
键。 alert
值是 body
和 severity
.
的散列
在您的控制器中,您引用了 params[:body]
和 params[:severity]
。那些应该是 params[:alert][:body]
和 params[:alert][:severity]
.
有没有理由不使用 Rails' 强参数?您可以重构为:
def create
@alert = Alert.new(alert_params.merge(agency_id: @current_member.id)
…
end
…
private
def alert_params
params.require(:alert).permit(:body, :severity)
end
在 create method
参数中应该是这样的。
因为你的参数 "alert"=>{"body"=>"This is the body text.", "severity"=>"12345"}
def create
@alert = Alert.new(body: params[:alert][:body],
severity: params[:alert][:severity],
agency_id: @current_member.id)
respond_to do |format|
if @alert.save
format.html { redirect_to @alert.agency, notice: 'Alert was successfully created.' }
format.json { render :show, status: :created, location: @alert}
else
format.html { render :new }
format.json { render json: @alert.errors, status: :unprocessable_entity }
end
end
end
我的 alerts/views/ 文件夹中有一个名为 _form.html.erb 的部分。我从我的 agencies/views/ 文件夹中的显示页面呈现部分内容。
一切都正确呈现,部分确实创建了一个新警报,但警报完全是空的。
alerts_controller 创建方法
def create
@alert = Alert.new(body: params[:body],
severity: params[:severity],
agency_id: @current_member.id)
respond_to do |format|
if @alert.save
format.html { redirect_to @alert.agency, notice: 'Alert was successfully created.' }
format.json { render :show, status: :created, location: @alert}
else
format.html { render :new }
format.json { render json: @alert.errors, status: :unprocessable_entity }
end
end
end
_form.html.erb
<%= form_for(Alert.new) do |f| %>
<div class="input-group">
<div class="field">
<%= f.label :body %><br>
<%= f.text_field :body %>
</div>
<div class="field">
<%= f.label :severity %><br>
<%= f.number_field :severity %>
</div>
<div class="action">
<%= f.submit %>
</div>
</div>
<% end %>
没有错误,只是创建的警报完全是空的。主体、严重性和 agency_id 变量均为零。
我试过更换线路
<%= form_for(Alert.new) do |f| %>
有了这个:
<%= form_for(@alert) do |f| %>
并添加此行:
@alert = Alert.new
代理控制器中的显示方法。
但是无论哪种方式都会发生同样的事情。我做错了什么?
编辑
这是我点击提交时开始的日志,在 alerts.create 方法中加载重定向之前结束。
Started POST "/alerts" for ::1 at 2016-03-31 18:29:43 -0400
Processing by AlertsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CgpepnYUec9uUoE/Ys6SAkOlzmK+w2q9IN782sXNoXnB3UyuegiS6m+W+mW+nXu4EIL8P8xH6JdigU8FfmzhVw==", "alert"=>{"body"=>"This is the body text", "severity"=>"12345"}, "commit"=>"Create Alert"}
Account Load (0.1ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = ? LIMIT 1 [["id", 7]]
Agency Load (0.1ms) SELECT "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1 [["id", 2]]
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "alerts" ("agency_id", "created_at", "updated_at") VALUES (?, ?, ?) [["agency_id", 2], ["created_at", "2016-03-31 22:29:43.014846"], ["updated_at", "2016-03-31 22:29:43.014846"]]
(135.8ms) commit transaction
Agency Load (0.1ms) SELECT "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1 [["id", 2]]
Redirected to http://localhost:3000/agencies/2
Completed 302 Found in 141ms (ActiveRecord: 136.2ms)
当我注释掉 alerts.create 方法并添加这一行时:
render plain: params
这是输出:
{"utf8"=>"✓",
"authenticity_token"=>"3OfteFX41SV/5NxpTcKbP7AKhLm/ZKah+NXVn84e2xwXMP9wWeQ+AH4gpzORkXKF4y225M3gJIu6imZAdb+bMg==",
"alert"=>{"body"=>"This is the body text.", "severity"=>"12345"},
"commit"=>"Create Alert", "controller"=>"alerts", "action"=>"create"}
从您的 params
调试中可以明显看出根本原因。 params
散列中有一个 alert
键。 alert
值是 body
和 severity
.
在您的控制器中,您引用了 params[:body]
和 params[:severity]
。那些应该是 params[:alert][:body]
和 params[:alert][:severity]
.
有没有理由不使用 Rails' 强参数?您可以重构为:
def create
@alert = Alert.new(alert_params.merge(agency_id: @current_member.id)
…
end
…
private
def alert_params
params.require(:alert).permit(:body, :severity)
end
在 create method
参数中应该是这样的。
因为你的参数 "alert"=>{"body"=>"This is the body text.", "severity"=>"12345"}
def create
@alert = Alert.new(body: params[:alert][:body],
severity: params[:alert][:severity],
agency_id: @current_member.id)
respond_to do |format|
if @alert.save
format.html { redirect_to @alert.agency, notice: 'Alert was successfully created.' }
format.json { render :show, status: :created, location: @alert}
else
format.html { render :new }
format.json { render json: @alert.errors, status: :unprocessable_entity }
end
end
end