问题
I have this form in /new.html.erb:
<%= 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 %>
and these Vendor methods:
def new
@vendors = Vendor.all
@vendor = Vendor.new
end
def vendor_params
params.require(:vendor).permit(:id, :name, :latitude, :longitude, :address, :image)
end
When I try to render the page, I get this error:
undefined method `merge' for "Store Name":String
Extracted source (around line #8):
5: <h4>New Vendor Form</h4>
6:
7: <%= form_for @vendor, :html => {: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" %>
I'm not sure why merge
is being called here. Is it in a hidden field possibly? Most of the answers I found pertaining to this error had to do with hidden fields. I'm using Geocoder to geocode lat and long after validation like so (not sure if this is relevant):
class Vendor < ActiveRecord::Base
geocoded_by :address
after_validation :geocode,
:if => lambda{ |obj| obj.address_changed? }
end
Any help here is much appreciated! Thanks in advance.
回答1:
you should write like this because your 2nd parameter should be an option hash, it should not be a value..
<%= form_for @vendor, multipart: true do |f| %>
<%= f.text_field :name, :value => "Store Name" %>
<%= f.text_field :address, :value => "Store Address" %>
<%= f.file_field :image %>
<%= f.submit "Save", class: "btn btn-success" %>
<% end %>
回答2:
Try changing your form like below
7: <%= form_for @vendor, :html => {:multipart => true} do |f| %>
8: <%= f.text_field :name %>
9: <%= f.text_field :address %>
10: <%= f.file_field :image %>
11: <%= f.submit "Save", class: "btn btn-success" %>
the second argument is part of the text_filed name which in your case will mess up your parameters
来源:https://stackoverflow.com/questions/19546040/undefined-method-merge-for-xxxxxxstring-rails-form-for