This list of Frequently Asked Questions(FAQ) is divided into the following sections:
Rails 2.1 was released on June 01, 2008.
A quick discussion of the new features is available in the article: Rails 2.0: It’s done!
These two reports show some (not all) of the remaining issues that did not make it into Rails 1.1:
See License.
R for Ruby, a play on Struts, and I liked the sound of it ;).
Although not released until the 24th of July 2004, Rails has been in development since the Basecamp project started in mid/late 2003 (from http://www.loudthinking.com/arc/000446.html)
There are a few! See the BookList.
See RailsStatistics.
return to top
See Plugins (as of Rails 0.14)
Ruby 1.8.4 is recommended.
To check your Ruby version type…
> ruby --version
Apache and Lighttpd are the most popular web servers for running Rails. But any web server that supports mod_ruby (Apache), CGI, SCGI, or FastCGI? can be used.
Apache is the reference web server and will be the one upon which all examples are based.
You may also want to consider using Lighttpd which is comparable in speed to Apache 2.0.54 using MPM Worker on Debian 3.1—this is considerably faster than Apache 1.x or 2.x MPM Prefork. WARNING: Lighttpd-1.4.10 is very buggy—use Lighttpd-1.4.11 or newer version.
A newer option is to use Zed Shaw’s Mongrel . Mongrel is a ruby based webserver that uses C extensions to dramatically improve performance compared to Webrick. Mongrel can be run independently or in a cluster behind a Lighttpd or Apache proxy.
We’ll divide the answer in 2 parts (FastCGI and non-FastCGI).
For FastCGI:NOTE: mod_fcgid is very active and mod_fastcgi hasn’t been updated in years.
For non-FastCGIvhosts.conf?Currently, no. Do have a look at hacking vendor/railties/webrick_server.rb if you want to give it a shot.
$_SERVER variable where you can access $_SERVER['HTTP_USER_AGENT'] and other details?Rails has request.env, which is quite similar. See request.env in the API docs for more details (under the “Request” section)
Here’s also a list of VariablesInRequestEnv
script/new_controller. However, when I try to open <a href="http://localhost">http://localhost</a>:3000/interviews I get a Webrick 404 error. Is the URL rewriting not working in the webrick version? If not, how should I do it instead?Make sure that you put a trailing slash after your controller, like http://localhost:3000/interviews/
<a href="http://railsserver/somecontroller/">http://railsserver/somecontroller/</a> to access the index of somecontroller. Is there a way around this?This happens automatically with Rails 0.6.0 (not yet with WEBrick, though).
You can use redirect_to :back (which depends on request.env['HTTP_REFERER'] being available) or use session: @session[:return_to] = @request.request_uri from sender, then redirect_to @session[:return_to] on target.
Static files should go in the public folder. It is best to put images in public/images, stylsheets in public/stylesheets and javascripts in public/javascripts. This will allow you to use the built-in Helpers more effectively. In your rhtml, the url’s would be relative to public. Thus it’s /images/image.png not /public/images/images.png.
Rails previously used symlinks. Now it’s recommended that you just populate those real directories in public with your files. And then include the public directory in what you would check into CVS or Subversion.
return to top
The session is part of the controller layer, which sits above the model layer. While the controller layer relies on the model layer, the model layer should be independent from the controller layer.
If there is a specific piece of information a model needs to implement some of its methods, the right approach is to provide an attr_accessor for that piece of information in the model. Controller code can then set that attribute on the model before using any methods that depend on it.
If you seem to need session information all over your models, you are probably trying to put controller code into your models; you should rethink your design.
Yes, several good ones. You should only ever store a model’s id in the session. See sessions for such pitfalls and more information on the session in general. That said, there is a good workaround by François Beausoleil.
return to top
belongs_to and has_one?The difference has to do with which table has the foreign key. See an illustration of ActiveRecordAssociations. For more details see the paragraph titled belongs_to or has_one from the API documentation.
@post = @author.post.build
@post.attributes = @params['post']
@post.save@
It will automatically fill the foreign key if it can. Of course @author has to exist in the db at this point.
return to top
As of Rails 0.12 most repetitive queries can be avoided by making use of EagerLoadingOfAssociations. See also PiggyBackQuery for an explanation of how to use your own SQL where appropriate.
On CGI, everything reloads on every request. On FastCGI? and mod_ruby, AR will cache the column information.
As of Rails 0.12 See benchmarking and profiling scripts have been available. See BenchmarkSuiteWish for more details.
return to top
Because I still need to names of the variables to properly construct the form element, so you’d quickly end up with a lot of input looking like text_field @post.title, "post", "title"—instead of just saying it once.
No, but you can move the helper method into the controller and add helper_method :method_name after the definition to make it available to the view again.
Rake will report a number of statistics on your codebase
$ rake stats
Given a situation where you have something like in your view:
<% @posts.each do |post| %>
The #each method can be replaced with #reverse_each which will traverse the array in reverse order.
<% @posts.reverse_each do |post| %>
link_to ?link_to "new", { :action => 'new' }, 'class' => 'inner'
See the API Docs for link_to for more details.
Make those methods private or protected.
See WhatGetsPluralized.
see 10 reasons Rails does pluralization
I couldn’t even get them working until I found this post:
http://www.loudthinking.com/arc/000362.html
and realized I needed to add something like observer :my_observer to the application.rb. Is that even the right place to put it??
For normal ruby, id is deprecated in favour of object_id. For \ActiveRecord, id always does read_attribute(primary_key).
<%= ... %>? render_text? printf?concat(string, binding) can be used if needed. See the API docs for #concat in “ActionView::Helpers::TextHelper”
No. That doesn’t mean it can’t be done.
(I’m still just getting started with Rails, but I’m thinking maybe this would be something you could use a filter for. The filter would scour the output for internal links and attach a session ID to the end of all of them. This ID might need to appear after a question mark in the URL so that stupid search engines that didn’t keep track of cookies wouldn’t think they were scouring different pages.)
-%> different from %> ?The extra dash swallows the rest of the line (no newline in the output).
<%- different from <% ?Apparently nobody knows.
See OfflineDocumentation.
return to top
See DatabaseDrivers. Many people prefer SQLite 3.x for development and either MySQL or PostgreSQL for production. Several others, including non-free commercial databases, are also supported by Rails.
ModelSecurity provides access control within the data model so you can specify read/write access for individual columns. It is a free, 3rd-party Rails Generator created by Bruce Perens which can easily be installed as a rubygem.
Don’t give your fields the same name as any of your Active Record classes or tables. For instance don’t use flavor in your candies table, if Flavor descends from Active Record::Base. Use flavor_id (or anything other than flavor) instead.
See database.yml. You will most likely have to run `locate mysql.sock` or `locate mysqld.sock` to find where your socket is located, then update database.yml to reflect your system’s configuration.
Yes it does. If you have login problems create a user and use the OLD_PASSWORD function to generate a compatible password. This is generally not a problem.
Use a tinyint(1). See HowtoUseBooleanColumns for more details.
ENUM column types?No, not currently.
Packets out of order with \MySQL 4.1See MySQL Database access problem
See MagicFieldNames.
<a href="http://wiki.rubyonrails.com/rails/pages/ActiveRecord" class="existingWikiWord">ActiveRecord</a>::ConnectionFailed in Finance#list
undefined method `show_datatypes=' for #<SQLite::Database:0xf6dcf8f0>
As per directions in HowtoUseSQLite, make sure you are using sqlite-ruby and not sqlite from the gems. For me that was ‘2. sqlite-ruby 2.2.3 (ruby)’ on ‘gem install sqlite’.
NOTE: Use sqlite3-ruby (1.1.0) and SQLite 3.3.4 (or later) for best results.
There are 3, yes three, different postgresql drivers available for ruby (and rails).
1. postgres is old and stale but reliable
2. ruby-postgres is new and improving and beta
it is essentially postgres + improvements
3. postgres-pr is pure ruby, slowest, runs anywhere
For rails 1.0 and 1.1, use ‘postgres’ or ‘postgres-pr’.
For rails 1.1.1 and beyond, try ‘ruby-postgres’ first to see if it works because that will be preferred in the future. You can always fall back to ‘postgres’ if it doesn’t work for you.
now() within SQL statements yield transaction-consistent timestampts (i.e. now() always returns the timestamp when the transaction started). I can still do this with AR by making an explicit UPDATE after saving, but is there any way to set AR date fields to some magic object that will result in now() being used in the INSERT statement generated by ActiveRecord::Base.save?Within PostgreSQL, the CURRENT_TIMESTAMP family of functions returns the time of the start of the current transaction. The exception to this rule is the timeofday() function, which always returns the wall-clock time.
There was a thread on the mailing list about this,
From this thread, you can deduce that this is simply a misunderstanding about what PostgreSQL is supposed to do.
UPDATE: see ticket #4461 which provides a patch that is likely to be included with rails-1.1.1.
Currently, url_for is not accessible in mailer classes. The reason is
that url_for requires some infrastructure that is not available in
mailer classes. Until someone submits a patch that changes that, the
only way to take advantage of url_for in mailer classes is to do
something like :
# in the controller
url = url_for(...)
SomeMailer.deliver_some_mail(url)
And then use the passed value to help render the mail.
Or alternatively check out:
http://wiki.rubyonrails.org/rails/pages/HowtoUseUrlHelpersWithActionMailer/
return to top
I noticed this problem when I wrote a very small demo program using ajax and using the page.insert_html method.
--------- app/views/main/index.rhtml ------------ <h3>AJAX Demo</h3> <ul id="my_list"> <li>Original item... please add more!</li> </ul> <% form_remote_tag (:update => 'my_list', :position => 'top', :url => {:action => 'do_rjs_stuff'}) do -%> </br> <%= submit_tag "submit" %> <% end %> --- app/controllers/main_controller.rb -------- class MainController < ApplicationController def do_rjs_stuff render :update do |page| page.insert_html :before, 'my_list', "<li>" + "Bob"+ "</li>" end end end -----------------------------------------------
The problem turned out to be my use of :update
and :position in form_remote_tag. I guess
that because page.insert_html is already
specifying where the string should go (ie
:before) it is wrong to put it in
form_remote_tag also. Hence I rewrote my view
to this:
<h3>AJAX Demo</h3> <ul id="my_list"> <li>Original item... please add more!</li> </ul> <% form_remote_tag (:url => {:action => 'do_rjs_stuff'}) do -%> </br> <%= submit_tag "submit" %> <% end %>
Now with this change the action worked as expected.
Answer: You need <%= periodically_call_remote (...) %> otherwise the relevant javascript code is not inserted into your html code.
How is the deprecations page http://www.rubyonrails.org/deprecation useful in any sort of way? There is no actionable information listed! Where do we find useful information about deprecated features and how to migrate code to newer APIs. I’ve inherited a sprawling old rails 1.0.0 app that I have to migrate to 1.2.3.