1

我有这些相关的控制器模型:

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)
4

3 回答 3

3

问题是您没有form_for正确使用发送参数。您直接使用form_tag,text_field_tag等,您应该只在特定情况下使用,因为这会绕过 Rails 的大部分内置功能。您不需要指定一个值,因为它会自动由@vendor. 您可以制作标签来标记每个字段,如下例所示:

<%= form_for @vendor, html: {multipart: true} do |f| %>
  <%= f.label :name, "Store Name" %>
  <%= f.text_field :name %>
  <%= f.label :address, "Store Address" %>
  <%= f.text_field :address %>
  <%= f.file_field :image %>
  <%= f.submit "Save", class: "btn btn-success" %>
<% end %>

在您的new操作中确保您实例化一个空Vendor对象,以便它知道在提交表单时将表单发送到哪个控制器/操作。

如果您不想使用标签来标记字段,则可以使用 html5placeholder属性来提供字段提示:

<%= f.text_field :name, placeholder: "Store Name" %>
于 2013-10-21T22:57:47.103 回答
2

你的问题在这里:

<%= form_tag({controller: "vendors", action: "create"}, method: "post", multipart: true) do %>

具体来说,当您strong params在 Rails 中“要求”使用参数时,它基本上期望参数哈希以这种方式构造:

params{"vendor" => {"name":sdfadsf, "address": 23423}}

只有在 new.html.erb 视图中使用才能实现form_for @vendor这一点,因为 Rails 就是这样定义参数哈希的初始值的。您的应用正在做的是将参数哈希设置为:

params{"name": 234234, "address: 234324}

解决方案

在您的new.html.erb表单中,只需使用以下代码:

<%= form_for @vendor, 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 %>
于 2013-10-23T09:48:55.613 回答
0

除非我错过了一些不是很明显的东西——

params.require(:vendor)

您指定参数必须包含 :vendor。只需删除此子句,例如

def vendor_params
  params.permit(:id, :name, :address, :image, :latitude, :longitude)
end

如果您的参数嵌套在下面,:vendor那么这.require(:vendor)将是有意义的。查看您的开发日志以查看传入的参数(如果您仍然遇到问题,请在此处分享)。

于 2013-10-21T22:50:49.323 回答