undefined method `merge' for “xxxxxx”:String rails form_for

坚强是说给别人听的谎言 提交于 2020-01-04 14:30:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!