热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

面试准备

Ruby On Rails 面试题与答案

问题 36. How does Validation works in Ruby on Rails?

Validation means checking to see if data  is good before it is stored in the database.

During signups and other such user input cases you want to check and be sure that the data is validated. In the past developers would often put this type of validation logic as triggers in the database.  

In an MVC architecture one can do validations at each level. 

You can do validations in the controllers but it is usually a good idea to keep your controllers skinny.

Views suffer from the javascript limitation because javascript can be disabled on the client side so they are not completely reliable.

The best way to manage validation is to put it in the model code. This model code is really the closest as you can be to the database and works very well for Rails applications.

Here are a few validation examples:

class Person < ActiveRecord::Base
  validates :name, :length => { :minimum => 2 }
  validates :points, :numericality => { :only_integer => true }  # only integer
  validates :age,  :numericality => { :greater_than => 18 } # greater than 18
  validates :email, :uniqueness => true
  validates :email, :confirmation => true  # this is to validate that the two email fields are identical
  validates :email_confirmation, :presence => true # this is to validate that the email confirmation field is not nil

In your view template you may use something like this:

 <%= text_field :person, :email %> <%= text_field :person, :email_confirmation %>

这有帮助吗? 添加评论 查看评论
 

问题 37. How can you add custom validation on your model in Ruby on Rails?

Now custom validations takes it to the next step.

Say you want to confirm that the data meets certain criteria 

这有帮助吗? 添加评论 查看评论
 

问题 38. What is Flash in Ruby on Rails?

Flash  is simply a way to pass some value to the next action. 

Anything you place in the flash will be exposed to the very next action and then cleared out.

Here’s an example:

def destroy

section = Section.find(params[:id])      
section.destroy      
FLASH[:NOTICE] = "SECTION DESTROYED."     
redirect_to(:action => 'list', :page_id => @page.id)    

end

Then wherever you want to use the flash you can write this code. I often put this snippet in the application.html.erb file, somewhere towards the top:

   <% if !flash[:notice].blank? %>         
<div class="notice">  
 <%= flash[:notice] %>

这有帮助吗? 添加评论 查看评论
 

问题 39. HOW CAN YOU INSTALL THE MISSING GEMS THAT ARE REQUIRED BY YOUR APPLICATION IN THE SIMPLEST WAY in Ruby on Rails?

bundle install

这有帮助吗? 添加评论 查看评论
 

问题 40. How can you implement internationalization in Ruby on Rails?

Ruby ships with i18n which is an internationalization gem. 

You need to create locale files and save them under the config/locales directory as:

en.yml

es.yml

fr.yml

The keys should match for each of these files. 

en:   main_page:     hello: “Hello”     welcome: “Welcome to   My Company” es:   main_page:     hello: “Hola”     welcome: “Bienvenido a Mi Empresa” fr:   main_page:     hello: “Salut”     welcome: “Bienvenue à  Mon Entrepriseâ€

 In your code you would need to specify that the text would be locale specific. So change it to something like this:

.content   %h1     = t("main_page.hello")  
%p     = t("main_page.welcome")

Then you have to select the actual locale.

这有帮助吗? 添加评论 查看评论
 

用户评价最有帮助的内容:

版权所有 © 2026,WithoutBook。