form_for wrong number of arguments in rails 4 - ruby-on-rails

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 %>

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.

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!

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 ?

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

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

Related

Rails "Browser Gem" support

I'm new to Rails, I'm trying to get the "browser" gem working.
(https://github.com/fnando/browser)
I was hoping someone had some example code they code post. I installed the gem by putting gem "browser", require: "browser/browser" in my gemfile.
I ran gem install browser
So I don't really know how to use the gem to actually get the users browser info. I tried just doing this as a test in the view <%= browser.full_version %> but I get Template::Error (undefined local variable or method browser'
any example code on what to put in a view/modal/controller would be appreciated, thanks.
You need to instantiate the object. So you can do something like this:
def index
#browser = Browser.new(request.env["HTTP_USER_AGENT"])
end
Then you can call the object in your view:
Device: <%= #browser.device.name %><br>
Platform: <%= #browser.platform.name %><br>
Info: <%= #browser.to_s %><br>
PS. I tried using this gem but found useragent which seemed to better display the information I wanted.
You can simply install the gem and "call it" in your view.
For example:
<% if browser.device.mobile? || browser.platform.android? %>
To install this gem you need:
add gem declaration to Gemfile
run bundle install from the console
restart your development server
P.S. when you are starting to use any gem, you should carefully read gem's README first. In 95% cases they are documented good enough.

Bootstrap 3 gem on rails 3.2 form distorted

Using this gem: https://github.com/anjlab/bootstrap-rails
For some reason the forms are not working properly for me. There is not curves when the input is being used.
And I cannot add classes to it like 'form-inline'
Not sure what the problem is. Anyone else facing this/have a solution?
After digging further, and deciding to trial and error, I got it working but have to manually input the classes for each form.
<%= text_field_tag :twitter_contact, nil, class: 'form-control'%>
Now i have this:
Why is the gem not working automatically to do that itself? Am I missing something?
You can write you own helper methods that include this for you. For example
def my_text_field_tag(name, value)
text_field_tag(name, value, class: 'form-control')
end
in your ApplicationHelper would give you something closer to what you're looking for.
The fact is that Ruby gem authors have to be careful injecting too much auto-magic into their code, as it actually conflicts with other Ruby gems. To a certain extent, you have to learn how Ruby works in order to get the full magic that you're looking for.

Exactly which content-inserting block helpers have changed behaviour in Rails 3?

The release notes for Rails 3.0 include this change:
7.4.2 Helpers with Blocks
Helpers like form_for or div_for that insert content from a block use <%= now:
<%= form_for #post do |f| %>
...
<% end %>
Your own helpers of that kind are expected to return a string, rather than appending to the output buffer by hand.
Helpers that do something else, like cache or content_for, are not affected by this change, they need <% as before.
We're in the process of migrating a web application from Rails 2.3.18 to Rails 3.1.12, and it would be very useful to have a complete list of such helpers that have changed, so that we can check all of their occurrences in our source code, but I'm having trouble finding an authoritative list of this kind.
I've tried looking through the git history of the rails project, but there seem to be many commits with related changes, and they're not obviously grouped on particular branch. For example, it seems to be clear that this list includes:
form_for
form_tag
fields_for
field_set_tag
... from 7b622786f,
link_to
... alluded to in e98474096 and:
div_for
content_tag_for
... alluded to in e8d2f48cff
remote_form_for
.... alluded to in 0982db91f, although it's removed in Rails 3.
However, I'm sure that's not complete - can anyone supply a complete list?
i don't have a complete list, but i think that you can derive most of what has changed from having a look at the diff in documentation of UrlHelper and FormHelper. most of the methods in those helpers changed to the new syntax.
http://apidock.com/rails/v2.3.8/ActionView/Helpers/UrlHelper/link_to
http://apidock.com/rails/v2.3.8/ActionView/Helpers/FormHelper/form_for
There is a list of these methods in the rails_upgrade plugin, whose purpose is to check your application for problems on upgrading from Rails 2 to Rails 3. The relevant method is check_old_helpers, which checks for block helpers containing any of:
content_tag
javascript_tag
form_for
form_tag
fields_for
field_set_tag
As for how authoritative this is, this plugin is an official Rails project plugin, although it does miss out a couple that I found by searching the git history:
div_for
remote_form_for
link_to
However, if the official tool for checking for these helpers is missing some, perhaps this is as good a list as I'm likely to find. Another point is that the upgrade check tool mentions that there should be deprecation warnings if you miss some, which provides an additional check:
Block helpers that use concat (e.g., form_for) should use <%= instead of <%. The current form will continue to work for now, but you will get deprecation warnings since
this form will go away in the future.

How to use Rails I18n.t to translate an ActiveRecord attribute?

Trying to use my active record translations in config/locales/de.yml also in my views. I thought I am clever using this:
de:
activerecord:
attributes:
user:
login: "Benutzerkennung"
comment: "Bemerkungen"
And in my view this:
<%= label_tag :login, t('activerecord.attributes.user.login') %>
But instead of the translation value ("Benutzerkennung") I am getting the infamous
"translation missing: de, activerecord, attributes, user, login"
Has anybody got this working (not using the label translation plugin (I am wary of potential side effects), or User.humanize_attribute_name)? What am I missing? (it does work when I use "activerecord1" or something else than activerecord, so my setup seems to be fine)
Thanks!
Ok, my bad, it does work just fine. I fell in the YML formatting trap :(
To help you debug along the way, use "script/console" and the following statements:
- I18n.locale --> should output the locale you want to examine
- I18n.t('activerecord.attributes') --> should give you all the key/value pairs for your translation, if not, you made a formatting error in your YML file or it could not be found
And btw - the plugin works quite well http://github.com/iain/i18n_label/
if you don't like the result of ".human_name" (which the plugin uses), just fall back to I18n.t('your key')
Another method:
<%= label_tag :login, User.human_attribute_name(:login) %>
You should upgrade Rails gem to v2.3.11 (I tried to use 2.3.9, but now days it is not available so I suggest you 2.3.11)!
gem install -v=2.3.11 rails
You can find this issues documented here: Rails 2.3.9 Release notes

Issue with Haml files

Hi i converting rails views from erb to Haml .The issue i faced is when use the form_for the haml throws the UNEXPECTED $end error. I am sure i did the space indentation well with in form_for .......Even if i use "each do" loop is says the same error. if i revert the haml to erb it works fine.
Rails version i used : 2.3.2 & i installed haml gem 2.2.19 as well as haml plugin also.
my controller code :
def new
#user = User.new
end
My View code :
.contentContainer.signup
- form_for(#user) do |f|
Make sure your haml indentation is perfect.
.contentContainer.signup
- form_for(#user) do |f|
= f.text_field :name
Are you including - end in your templates? Haml takes care of ends for you, so if you add your own, it won't work.
Can you paste your entire template (in a code block, so it's formatted properly)?
There's also a good command-line tool to make transition easier: html2haml. It doesn't always produce the prettiest haml, but it certainly works.
Hey, there's even a web-based form for this: http://html2haml.heroku.com/
Generally, be sure your indentation is perfect. haml is very particular about indentation. If you use a decent editor (such as textmate or vim) this is an easy task.
If the last line in file is indented there has to be an addidtional, empty line.

Resources