EzDevInfo.com

ruby-on-rails-4 interview questions

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

Active admin install with Rails 4

I got this error when installing active admin on Rails 4

Bundler could not find compatible versions for gem "actionpack":
In Gemfile:
  meta_search (>= 1.1.0.pre) ruby depends on
    actionpack (~> 3.1.0.alpha) ruby

  rails (= 4.0.0.rc1) ruby depends on
    actionpack (4.0.0.rc1)

I follow this instruction: http://www.activeadmin.info/docs/documentation.html

Anyone help please.


Source: (StackOverflow)

What is `params.require(:person).permit(:name, :age)` doing in Rails 4?

All the examples of strong parameters in Rails 4 docs use params.require(:person).permit(:name, :age).

Could someone please deconstruct and explain what is occurring with require and permit here?

Thanks!


Source: (StackOverflow)

Advertisements

How to use concerns in Rails 4

The default Rails 4 project generator now creates the directory "concerns" under controllers and models. I have found some explanations about how to use routing concerns, but nothing about controllers or models.

I am pretty sure it has to do with the current "DCI trend" in the community and would like to give it a try.

The question is, how am I supposed to use this feature, is there a convention on how to define the naming / class hierarchy in order to make it work? How can I include a concern in a model or controller?


Source: (StackOverflow)

Rails I18n validation deprecation warning

I just updated to rails 4.0.2 and I'm getting this warning:

[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.

Is there any security issue in setting it to false?


Source: (StackOverflow)

Rails 4: before_filter vs. before_action

In rails >4.0.0 generators creates CRUD with before_action not before_filter. It seems to do the same thing. So what the difference?


Source: (StackOverflow)

How is attr_accessible used in Rails 4?

attr_accessible seems to no longer work within my model.

What is the way to allow mass assignment in Rails 4?


Source: (StackOverflow)

Rails 4 datatypes?

Where can I find a list of data types that can be used in Ruby on Rails 4 (such as text, string, integer, float, date, etc)?

I keep learning about new ones but I'd love to have a list I could easily refer to.


Source: (StackOverflow)

How to reference images in CSS within Rails 4

There's a strange issue with Rails 4 on Heroku. When images are compiled they have hashes added to them, yet the reference to those files from within CSS don't have the proper name adjusted. Here's what I mean. I have a file called logo.png. Yet when it shows up on heroku it is viewed as:

/assets/logo-200a00a193ed5e297bb09ddd96afb953.png

However the CSS still states:

background-image:url("./logo.png");

The result: the image doesn't display. Anybody run into this? How can this be resolved?


Source: (StackOverflow)

Guard with RSpec on Rails 4 giving a lot of warnings

I'm wondering how I can turn off all these warnings when running a simple test:

[1] guard(main)> 
16:59:46 - INFO - Run all
16:59:46 - INFO - Running all specs
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/rspec-rails-3.0.1/lib/rspec/rails/adapters.rb:124: warning: instance variable @example not initialized
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/rspec-rails-3.0.1/lib/rspec/rails/adapters.rb:124: warning: instance variable @example not initialized
.*

Pending:
  HomeHelper add some examples to (or delete) /Users/esjd/ruby/rails/ts3/spec/helpers/home_helper_spec.rb
    # Not yet implemented
    # ./spec/helpers/home_helper_spec.rb:14

Finished in 0.03601 seconds (files took 7 minutes 48 seconds to load)
2 examples, 0 failures, 1 pending
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/celluloid-0.15.2/lib/celluloid/tasks.rb:76: warning: global variable `$CELLULOID_DEBUG' not initialized
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/celluloid-0.15.2/lib/celluloid/tasks.rb:76: warning: global variable `$CELLULOID_DEBUG' not initialized

/Users/esjd/.rvm/gems/ruby-2.1.2/gems/guard-2.6.1/lib/guard/interactor.rb:64: warning: instance variable @enabled not initialized
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/guard-2.6.1/lib/guard/interactor.rb:64: warning: instance variable @enabled not initialized
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/guard-2.6.1/lib/guard/interactor.rb:64: warning: instance variable @enabled not initialized
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/guard-2.6.1/lib/guard/interactor.rb:64: warning: instance variable @enabled not initialized
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/celluloid-0.15.2/lib/celluloid/tasks.rb:76: warning: global variable `$CELLULOID_DEBUG' not initialized
/Users/esjd/.rvm/gems/ruby-2.1.2/gems/celluloid-0.15.2/lib/celluloid/tasks.rb:76: warning: global variable `$CELLULOID_DEBUG' not initialized

I'm running Rails 4.1.1, Ruby 2.1.2p95, and...

guard (2.6.1)
rspec (3.0.0)
guard-rspec (4.2.9)

I've tried running guard with:

guard :rspec, cmd:"ruby -W0 bin/rspec" do

instead of:

guard :rspec, cmd:"spring rspec" do

But it didn't do anything.

These warnings are super annoying, and I'm pretty sure I'm not causing them. Help!


Source: (StackOverflow)

Rails 4: assets not loading in production

I'm trying to put my app into production and image and css asset paths aren't working.

Here's what I'm currently doing:

  • Image assets live in /app/assets/images/image.jpg
  • Stylesheets live in /app/assets/stylesheets/style.css
  • In my layout, I reference the css file like this: <%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
  • Before restarting unicorn, I run RAILS_ENV=production bundle exec rake assets:precompile and it succeeds and I see the fingerprinted files in the public/assets directory.

When I browse to my site, I get a 404 not found error for mysite.com/stylesheets/styles.css.

What am I doing wrong?

Update: In my layout, it looks like this:

<%= stylesheet_link_tag    "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag    "styles", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

The generate source is this:

<link data-turbolinks-track="true" rel='nofollow' href="/stylesheets/bootstrap.min.css" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" rel='nofollow' href="/stylesheets/styles.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-0c647c942c6eff10ad92f1f2b0c64efe.js"></script>

Looks like Rails is not properly looking for the compiled css files. But it's very confusing why it's working correctly for javascripts (notice the /assets/****.js path).


Source: (StackOverflow)

Jquery gets loaded only on page refresh in rails 4 application

I have created a rails 4 application and have added fancybox library for image popup effect. It works fine but only when the page is being refreshed.If the page is not refreshed by the user then the jquery does not works at all. I tried testing it with small jquery methods also but all work only after page refresh. I am also using twitter bootstrap.

My asset/application.js file :

//= require jquery
//= require jquery_ujs
//= require fancybox
//= require twitter/bootstrap
//= require turbolinks
//= require_tree .


$(document).ready(function() {
  $(".fancybox").fancybox();
    $("#hand").click(function(){
     if($("#check6").is(':visible'))
     {
      $("#check6").hide();
      }
     else
     {
      $("#check6").show();
      }
    });
});

Source: (StackOverflow)

Rails 4 - Strong Parameters - Nested Objects

I've got a pretty simple question. But haven't found a solution so far.

So here's the JSON string I send to the server:

{
  "name" : "abc",
  "groundtruth" : {
    "type" : "Point",
    "coordinates" : [ 2.4, 6 ]
  }
}

Using the new permit method, I've got:

params.require(:measurement).permit(:name, :groundtruth)

This throws no errors, but the created database entry contains null instead of the groundtruth value.

If I just set:

params.require(:measurement).permit!

Everything get's saved as expected, but of course, this kills the security provided by strong parameters.

I've found solutions, how to permit arrays, but not a single example using nested objects. This must be possible somehow, since it should be a pretty common use case. So, how does it work?


Source: (StackOverflow)

Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded

In my gemfile I have:

gem 'mysql2'

My database.yml is as follows:

default: &default
  adapter: mysql2
  database: <%= ENV['db_name'] %>
  username: <%= ENV['db_user'] %>
  password: <%= ENV['db_pass'] %>
  host:     <%= ENV['db_host'] %>
  pool: 32
  socket:   <%= ENV['socket'] %>

development:
  <<: *default

production:
  <<: *default

I've run both bundle update and bundle install and my Gemfile.lock shows mysql2.

However when I run rake db:migrate I get this on both my computer and on the staging server:

myproject.com(master)$ rake db:migrate
WARNING: Use strings for Figaro configuration. 10000012508 was converted to "10000012508".
WARNING: Use strings for Figaro configuration. 860526407370038 was converted to "860526407370038".
rake aborted!
Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
.....

Just to make sure there wasn't a bad version of mysql2 or something, I did bundle clean --force and ran bundle install and bundle update again and when I run gem list I see mysql2 (0.4.0) and no other versions.

Any ideas would be most appreciated.


SOLUTION

It's currently an issue with Rails 4.1.x and 4.2.x, per this bug report, it will be fixed in the next release of rails 4.2.x (credit to dcorr in comments for the link).

In the mean time you can fix by doing downgraded to version 0.3.18 of mysql2 by adding this line to your gemfile:

gem 'mysql2', '~> 0.3.18'

Source: (StackOverflow)

Rails 4 default scope

In my Rails app have a default scope that looks like this:

default_scope order: 'external_updated_at DESC'

I have now upgraded to Rails 4 and, of course, I get the following deprecation warning "Calling #scope or #default_scope with a hash is deprecated. Please use a lambda containing a scope.". I have successfully converted my other scopes but I don't know what the syntax for default_scope should be. This doesn't work:

default_scope, -> { order: 'external_updated_at' }

Source: (StackOverflow)

Rails 4 multiple image or file upload using carrierwave

How can I upload multiple images from a file selection window using Rails 4 and CarrierWave? I have a post_controller and post_attachments model. How can I do this?

Can someone provide an example? Is there a simple approach to this?


Source: (StackOverflow)