【发布时间】:2017-06-26 22:50:16
【问题描述】:
我正在尝试使用模式(来自物化)将对象添加到我的数据库中。当用户单击“新建”按钮时,将显示模态框并在填写表单后创建对象。
主视图是这样的
.
.
.
<%= link_to "add", new_item_path, remote: true %>
.
.
.
<div id="addItemModal" class="modal">
<div class="modal-content">
<h4 class="col s12">Add Item</h4>
<div class="row">
<%= form_with(model: Item.new, class: "col s12") do |f| %>
<div class="row">
<div class="input-field col s6">
<%= f.label :name %>
<%= f.text_field :name, class: "validate" %>
</div>
</div>
<% end %>
</div>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect waves-
green btn-flat">Cancel</a>
<%= button_to "Save",
items_path,
class: "modal-action modal-close waves-effect waves-green btn
light-blue darken-2 text-white", remote: true %>
</div>
</div>
当用户填写模态表单并点击提交时,在 items_controller 中调用 create 操作
def create
@item = Item.new(params[:item])
respond_to do |format|
if @item.save
format.js
else
format.js
end
end
end
我在提交后查看了 params 哈希,似乎输入到表单中的值没有发送到创建操作。我检查了输入字段,它们似乎已连接到对象
<input class="validate" type="text" name="item[name]">
编辑:
服务器响应
Started POST "/items" for 127.0.0.1 at 2017-06-26 17:59:23 -0500
Processing by ItemsController#create as JS
Parameters {"authenticity_token"=>"B1YVBtBy0NGe0jnhdeJquj19g8aJa8WfNwA/k8vekOySACiJ20wnR5XAI2rw7mIIUSnp5K7ZqmLxYdddGqimvw=="}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Redirected to http://localhost:3000/?message=Error+Saving
Completed 200 OK in 3ms (ActiveRecord: 0.1ms)
【问题讨论】:
-
您介意在单击提交时粘贴您在 rails 控制台中获得的内容吗?
-
我已经用服务器响应更新了我的问题
标签: jquery ruby-on-rails twitter-bootstrap materialize