form_for wrong number of arguments in rails 4

2020-08-26 11:13发布

问题:

I am using form_for tag and Its working in Rails 3.0.4 environment. But when I tried to update my project to Rails 4.It gives following errors

wrong number of arguments (3 for 2). Here is my code

   <%= form_for @email, :url => alerts_path do |f| %>
   <% end %>

回答1:

I can't see any issue with your form_for, can you post backtrace of exception ? may be another gem is overriding default form_for ?



回答2:

Try to remove things that may try to change things in views.

In my case the problem was with client_side_validations gem

Hope this helps.



回答3:

Not sure why yet, but removing the "meta_search" gem from my Gemfile fixed this exact problem for me on Rails 4.

If you google this problem you may find this GitHub issue that attributes it to the "client_side_validations" gem, but using the suggested "rails-4-quick-fixes" branch of that repo didn't fix the issue for me.

I think that the error with simple_form_for was a red herring, as I was getting the same error even using Rails built in form_for, and was getting wrong number of arguments (3 for 2) regardless of how many arguments I actually passed into either method.

meta_search hasn't had a new release since February 2, 2012, so I can only assume something in the gem wasn't behaving nicely with Rails 4. If I discover what the problem was specifically, I'll update this answer. It seems kind of lame to have to "just stop using" meta_search, but that's as much as I can offer so far. Thanks!



回答4:

Your syntax is correct. Try changing it like this:

<%= form_for(@email, url: alerts_path) do |f| %>
  #form fields goes here
<% end %>

I tried to see if there is any change in source-code for form_for in Rails 4.0 API; It's still the same.

Have a look at Line 262 at https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb

Let me know if it works



回答5:

Remove gem 'meta_search' from your gem file, and look into using gem ransack instead. https://github.com/activerecord-hackery/ransack