我有这些相关的控制器模型:
class VendorsController < ApplicationController
def vendor_params
params.require(:vendor).permit(:id, :name, :address, :image, :latitude, :longitude)
end
def create
vendor = Vendor.create(vendor_params)
flash[:success] = "Vendor Created"
redirect_to vendors_mgmt_path
end
end
当我尝试创建新的供应商对象时,出现以下错误:
Required parameter missing: vendor
这是我的表格:
<%= form_tag({controller: "vendors", action: "create"}, method: "post", multipart: true) do %>
<%= text_field_tag :name, "Store Name" %>
<%= text_field_tag :address, "Store Address" %>
<%= file_field_tag :image %>
<%= submit_tag "Save", class: "btn btn-success" %>
<% end %>
我对 Vendor 表的迁移:
class CreateVendors < ActiveRecord::Migration
def change
create_table :vendors do |t|
t.string :name
t.string :address
t.float :latitude
t.float :longitude
t.string :image
t.timestamps
end
end
end
有没有人看到我为什么会收到这个错误的线索?我不明白它:vendor
本身是一个参数......非常感谢任何帮助。提前致谢!
这是我的更新表格:
<%= form_for @vendor, multipart: true do |f| %>
<%= f.text_field :name, "Store Name" %>
<%= f.text_field :address, "Store Address" %>
<%= f.file_field :image %>
<%= f.submit "Save", class: "btn btn-success" %>
<% end %>
我没有收到此错误:
undefined method `merge' for "Store Name":String
从我所做的搜索来看,听起来 rails 方法merge
只适用于哈希,而不适用于字符串。我不确定为什么它会尝试合并它,除非我的表单没有正确创建哈希?
我的新动作:
def new
@vendors = Vendor.all
@vendor = Vendor.new
end
服务器日志
Started GET "/vendors/new" for 127.0.0.1 at 2013-10-23 08:04:04 -0700
Processing by VendorsController#new as HTML
Vendor Load (0.3ms) SELECT "vendors".* FROM "vendors"
Rendered vendors/new.html.erb within layouts/application (2.0ms)
Completed 500 Internal Server Error in 18ms
ActionView::Template::Error (undefined method `merge' for "Store Name":String):
5: <h4>New Vendor Form</h4>
6:
7: <%= form_for @vendor, multipart: true do |f| %>
8: <%= f.text_field :name, "Store Name" %>
9: <%= f.text_field :address, "Store Address" %>
10: <%= f.file_field :image %>
11: <%= f.submit "Save", class: "btn btn-success" %>
app/views/vendors/new.html.erb:8:in `block in _app_views_vendors_new_html_erb__2593637676836103609_2161482240'
app/views/vendors/new.html.erb:7:in `_app_views_vendors_new_html_erb__2593637676836103609_2161482240'
Rendered /Users/******/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.8ms)
Rendered /Users/******/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
Rendered /Users/******/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (11.9ms)