![]() |
|
|||||
|
View & Learn
|
News Feed
|
||||||
How does a web request work on Ruby on Rails architecture
1.)request goes to Controller -
2.) Controller calls the Model
3.) which interacts with database and returns control to Controller
4.)Controller then call the View which creates html
5.) View send HTML to the browser
What is the naming convention for code placed in the /lib directory
for a class called PdfStuff::Receipt the file would be receipt.rb in the directory lib/pdf_stuff
Naming convention for class names and file names
class names are written in CamelCase (each word beginning with a capital letter, with no spaces between words)
filename are written lowercase, with underscores separating each word
Syntax to grab a params value or a default if params is null
params[:var] || "default"
How would you do benchmarking in sections of code
wrapping the code in benchmarking class method
self.class.benchmark('descriptive name') do
...........................
end
How would you return text from the controller action to the browser
render :text => "stuff to send back"
Where do you put helper functions for a given controller
You put it in a file called controllername_helper.rb in the app/helpers directory
How can you make a method in controller public but don't want it to be accessible as an action
hide it using hide_action
:hide_action :method_name
What is the outcome of calling render(:action => ...)
Does not invoke the action method - it simply renders that action's default behavior
Controller method to send a data stream to the client
send_data
How do you set the cookie expiration time
:expires => option
Controller method to run a set of code before every action in controller
create a method then declare it as a before_filter
before_filter :method_name
Controller method to run a set of code before and after every action in controller
around_filter
use the keyword yield to indicate where the action should be run
Rails abstraction to allow verification that certain conditions are met before an action is attempted
verify mechanism ex:
verify :only = :post_comment, :session = :user_id, ......
How would you set up a block of code that acts like a switchboard for the different requests in controller file
respond_to do |wants|
wants.html { .......code for html page requestin here }
wants.js { .... code for AJAX request ....}
end
Contrast: private with protected methods
private are only available withing the classes in which they're stored, period
protected just like private but available to classes which inherit from the class in which the protected method is defined
How do you change the HTTP method generated by clicking a link
:method parameter
link_to 'Text of link', contact_url, :id => contact, :method => :delete
How is paging large records sets handled
on the controller you use paginate function when finding records @user_pages,
@users = paginate(......)
in the view file you use the paginate_links helper to put the paging links on page
pagination_links(....)
How can you created a cached portion on a view template
surround it with a cache block
<% cache do %>
...............
<% end %>
2 forms for ERb tag pairs
<%= ..... %> for regular output. The output of a Ruby expression between these tags will be displayed in browser
<%.... %> For code not intended to be displayed
error_messages_for 'Object'
Built-in helper function for Views that formats time difference in words
distance_of_time_in_words
time_ago_in_words
Built-in helper function for Views that format number as a currency
number_to_currency
