EzDevInfo.com

ruby-on-rails-3.2 interview questions

Top ruby-on-rails-3.2 frequently asked interview questions

Custom text for rails form_for label

I want to display a label in form_for:

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name %>
</div>

This generates the label "Name", but I want it to be "Your Name". How can I change it?


Source: (StackOverflow)

Adding a custom seed file

I want to populate a new feature with dummy data, but don't want to use the db/seeds.rb file as it already has seeds other data irrelevant for this feature.

To run the default seeds.rb file, you run the command rake db:seed.

If I create a file in the db directory called seeds_feature_x.rb, what would the rake command look like to run (only) that file?


Source: (StackOverflow)

Advertisements

ERROR: 'rake/rdoctask' is obsolete and no longer supported. Use 'rdoc/task' (available in RDoc 2.4.2+) instead

Seems the last post for this problem was closed for one reason or another so I'll try my luck...

I'm trying to run a simple "rake db:migrate" command. When I do, I get the error in the title. Yes, the solution "appears" obvious but it's not because I don't my Rakefile does not contain any references to 'rake/rdoctask'. It seems to be coming from documentation.rake but when I try to change that file as recommended by the error, I get a different error. I have found a couple of posts declaring this to be a known issue but nothing I've found indicates how to fix or work around this.

Does anyone have any ideas how to fix it? Below is the output from the rake command with a --trace:

$ rake --trace db:migrate
rake aborted!
ERROR: 'rake/rdoctask' is obsolete and no longer supported. Use 'rdoc/task' (available in RDoc 2.4.2+) instead.
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/rdoctask.rb:1:in `<top (required)>'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/tasks/documentation.rake:2:in `require'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/tasks/documentation.rake:2:in `<top (required)>'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/tasks.rb:15:in `load'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/tasks.rb:15:in `block in <top (required)>'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/tasks.rb:6:in `each'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/tasks.rb:6:in `<top (required)>'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/application.rb:213:in `require'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/application.rb:213:in `initialize_tasks'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/application.rb:139:in `load_tasks'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
    C:/developer/skknobara/Rakefile:7:in `<top (required)>'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_module.rb:25:in `load'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_module.rb:25:in `load_rakefile'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:583:in `raw_load_rakefile'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:89:in `block in load_rakefile'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:88:in `load_rakefile'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:72:in `block in run'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
    C:/Programs/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
    C:/Programs/Ruby193/bin/rake:23:in `load'
    C:/Programs/Ruby193/bin/rake:23:in `<main>'

I discovered that I had rails 3.0.3 in my Gemfile so I changed it to 3.2.9, but now when I run rake db:migrate I get:

rake aborted!
Bundler could not find compatible versions for gem "rack":
  In Gemfile:
    rails (= 3.2.9) x86-mingw32 depends on
      rack (~> 1.4.0) x86-mingw32

    rack (1.2.5)

I'm not sure what the error means but "gem list rack" shows I have both 1.4.1 and 1.4.0 installed. My Gemfile is pretty simple:

gem 'rails', '3.2.9'
gem 'rack','< 1.4.1'
gem 'mysql2', '< 0.3'

Any thoughts?


Source: (StackOverflow)

Rendering JSON in controller

I was reading a book and in a chapter about Controllers when it talks about rendering stuff, for JSON it has an example like this but doesn't go in to details so I couldn't figure out the bigger picture that this example fits in:

render :json => @projects, :include => tasks

And also some example with JSONP using it with callback functions:

render :json => @record, :callback => 'updateRecordDisplay'

Can someone explain these?


Source: (StackOverflow)

Where to put Ruby helper methods for Rails controllers?

I have some Ruby methods certain (or all) controllers need. I tried putting them in /app/helpers/application_helper.rb. I've used that for methods to be used in views. But controllers don't see those methods. Is there another place I should put them or do I need to access those helper methods differently?

Using latest stable Rails.


Source: (StackOverflow)

Use rvmrc or ruby-version file to set a project gemset with RVM?

I use RVM, the Ruby Version Manager to specify a Ruby version and a set of gems for each of my Rails projects.

I have a .rvmrc file to automatically select a Ruby version and gemset whenever I cd into a project directory.

After installing RVM 1.19.0, I get a message

You are using .rvmrc, it requires trusting, it is slower and it is not compatible with other ruby managers, you can switch to .ruby-version using rvm rvmrc to [.]ruby-version or ignore this warnings with rvm rvmrc warning ignore /Users/userName/code/railsapps/rails-prelaunch-signup/.rvmrc, .rvmrc will continue to be the default project file in RVM 1 and RVM 2, to ignore the warning for all files run rvm rvmrc warning ignore all.rvmrcs.

Should I continue using my .rvmrc file or should I switch to a .ruby-version file? Which is optimal? What are the ramifications?


Source: (StackOverflow)

No secret option provided to Rack::Session::Cookie warning?

I am running Rails 3.2.3, Ruby 1.9 under Fedora 17. I get this warning, when I run rails s, and how do I fix?

SECURITY WARNING: No secret option provided to Rack::Session::Cookie. This poses a security threat. It is strongly recommended that you provide a secret to prevent exploits that may be possible from crafted cookies. This will not be supported in future versions of Rack, and future versions will even invalidate your existing user cookies.


Source: (StackOverflow)

Reserved names with ActiveRecord models

I take naming pretty seriously, so I think pretty hard about good names for my ActiveRecord models. However, I frequently come up with a name and it has some conflict with a reserved name, either in the database or Ruby or Rails. Model or field names like set or group. Sometimes the issue isn't immediately apparent either. Is there a list somewhere of names we can't use in our model names and a list for field names?


Source: (StackOverflow)

Rails emits warning: "Rack::File headers parameter replaces cache_control after Rack 1.5"

I've recently upgraded my Rails to v3.2. The server starts and works, however I'm noticing the following warning:

Rack::File headers parameter replaces cache_control after Rack 1.5.

In my development.rb I have the following configuration for cache control:

config.static_cache_control = "public, max-age=36000"

What does the warning mean and what should I do to address it?


Source: (StackOverflow)

How to use Eclipse for Ruby on Rails (RoR)

I am new in Ruby. I have installed Ruby on Rails on Windows 7. I visited the following to choose a suitable IDE/Editor for writing Ruby code:

http://rubyonrails.org/download

I found the following Editors for Ruby:

VIM for Rails, RadRails, RubyMine, 3rd Rail, Netbeans for Ruby

I am familiar with Eclipse as I am using Eclipse for java. Can I use Eclipse for Ruby? If so, what configuration is necessary in Eclipse for Ruby project?


Source: (StackOverflow)

Server is already running in Rails

When I am starting rails server using rails s command it is showing A server is already running. Check C:/Sites/folder/Pids/Server.pids

When I open the file it is outputting a 4 digit number only so how could I resolve this issue ?

FYI

  1. No other instance of Rails cmd is running this time.
  2. Checked Task manager but only cmd.exe is showing no else process is running. (using Windows).

Source: (StackOverflow)

How to add confirm message with link_to Ruby on rails

I wanted to add confirmation message on link_to function with Ruby.

= link_to 'Reset message', :action=>'reset' ,:confirm=>'Are you sure?'

Any ideas why it's not working?


Source: (StackOverflow)

what is the difference between link_to, redirect_to, and render?

I am confused about the main difference between

link_to, redirect_to and render

in rails, anyone can please explain.


Source: (StackOverflow)

Rails 3.2 undefined method `key?' for nil:NilClass

For some reason I started to get this error after switching to Rails 3.2. I guess it has something to do with acl9 plugin, which I tried reinstalling, but nothing changed.

I moved the plugins to lib/plugins and added initializer to config/initializers but again, same error.

I looked for solution at acl9 repo on Github, but could not find anything there. Maybe it is not acl9 after all.

I have paperclip, acl9, authlogic installed.

NoMethodError (undefined method `key?' for nil:NilClass):
actionpack (3.2.1) lib/action_controller/metal/hide_actions.rb:36:in `visible_action?'
actionpack (3.2.1) lib/action_controller/metal/hide_actions.rb:18:in `method_for_action'
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:14:in `method_for_action'
actionpack (3.2.1) lib/action_controller/metal/compatibility.rb:61:in `method_for_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:115:in `process'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call'
journey (1.0.3) lib/journey/router.rb:68:in `block in call'
journey (1.0.3) lib/journey/router.rb:56:in `each'
journey (1.0.3) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__3140920687338355213__call__3168118505970967148__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.2.1) lib/rails/engine.rb:479:in `call'
railties (3.2.1) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/project/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/project/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/project/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

EDIT (SOLVED): It took quite a while to figure out and I am still not quite sure what happened. I think it has to do with acl9's support for Rails 3.1+ But in the end I took the acl9 code from github and everything suddenly started working.


Source: (StackOverflow)

Use active_model_serializer with a non-ActiveRecord object

I have a model object that is not descended from ActiveRecord::Base and is not stored in the database. I created a serializer for it (with the same name + "Serializer"), and in my controller I'm calling render json: object_instance.

The result is an exception from deep inside of render.

I implemented an as_json method that instantiates the serializer and calls it, but the result is then a missing method in the object, read_attribute_for_serialization.

I want my object to act like an ActiveModel-compatible object at least as far as Active Model Serializer goes, but I don't see any reference to this in their documentation.


Source: (StackOverflow)