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

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.

Related

initial migration with ruby project

When I run my initial migration I get a "new.html.erb" file for each of my models. Its a very simple looking form, but the "new.html.erb" contains just the following code..
<h1>New city</h1>
<%= render 'form' %>
<%= link_to 'Back', cities_path %>
Now I want to modify this page, but I dont really understand whats going on here, where in the project this 'form' is located?
The form is located under app/views/cities/_form.html.erb.
I strongly suggest you to read this page before start working with ruby - it will give you a better understanding of how things works.
Since Rais works on convention over configuration, you will have a bad time trying to figure out everything by yourself. However, once you are used to the conventions, your will have a very fast development speed.
In the same folder where your new view is located.

form_for wrong number of arguments in rails 4

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

How is <%= form_tag ... do %> implemented?

I'm trying to figure out how you can herald a Ruby block in a <%= ... %> emitter.
No problem with the '<% form_tag do %>' part, but as I dig into Rails internals
and see how it uses erb to process templates, the generated Ruby code is invalid,
due to that hanging 'do'. Is there a post-processor hiding in Rails somewhere that
straightens out the code before running it? If yes, where is it? If no, how does
Rails pull HTML and Ruby code out of this form?
Rails added a hack which uses a regular expression to figure out if what is passed to erb is a block expression and then handle it differently.
For a more detailed explanation: http://timelessrepo.com/block-helpers-in-rails3

Rails automatically escpaping HTML - how to stop it?

I'm working on upgrading an old Rails app (1.1.6) to Rails 3. Obviously, a lot has changed. One thing appears to be that Rails automatically escapes content dropped into the view. However, I have a situation where I have a helper generating IMG tags for me, and Rails is automatically escaping the resulting content.
<%= random_image('public/images/headers') %>
This results in escaped content, much like one would expect had I done this (in 1.1.6)
<%= h random_image('public/images/headers') %>
Is there a way to tell it to not escape?
<%= raw random_image('public/images/headers') %>
.html_safe
It may need to be inside the helper
There are there ways in which this can be achieved in rails 3 application
html_safe
raw
h
raw and h can only be used in controller and views these methods are defined in helpers.
html_safe can be used anywhere in a rails application i.e., can be used in models, helpers, controller etc.
For more information please read http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/

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