📜 ⬆️ ⬇️

Some Rails Tricks


I want to share in you a few tricks that will help to make working with Ruby on Rails better, faster, and also allow you to earn a lot and better dress.

1. Routes and console requests

Run the rails console and call the app method: now in the console you can do the same as in the integration tests

>> app.class => ActionDispatch::Integration::Session 


What does this give?
')
For example, you can test routes directly in the console:

 >> app.pages_path => "/pages" >>app.page_path(@page) => "/pages/345" 


Or make requests to our Rails application:

 >> app.get("/pages/345") => 200 >> app.response.body => "<!DOCTYPE html PUBLIC \'-//W3C//DTD XHTML 1.0 Transitional//EN\'\n ..." 


About all methods available through the app, you can read here ActionDispatch :: Integration :: Session and here: ActionDispatch :: Integration :: RequestHelpers

2. Helpers in the console

With the help of helper, we can access both the Rails built-in helpers:

 >>helper.mail_to "me@et.you" => "<a href=\"mailto:me@et.you\">me@et.you</a> >>helper.pluralize(2,'bug') => "2 bugs" 


so and to their:

 def give_me_blog_link <a href='myblog.com'>Blog</a> end 


 >> helper.give_me_blog_link => "<a href='myblog.com'>Blog</a> 


But what to do when instance variable is used in our helper (for example, client)?

 def os_adviser if @client.geek? 'free bsd' elsif @client.blond? 'mac os' else 'windows' end end 


The problem can be solved by using Object # instance_variable_set

 >> helper.instance_variable_set :@client, Client.first => #<Client id: 3034, ... >> helper.os_adviser => "windows" 


Ok, so what if the helper uses data from params?

 def do_you_want_meat params['meat'] ? ' ,   !' : ', ,   ' end 


This will help us a small hack:

 >>helper.controller = OpenStruct.new( { :params => {} } ) => #<OpenStruct params={}> >>helper.do_you_want_meat => ', ,   ' >>helper.controller = OpenStruct.new( { :params => { :meat => true } } ) => #<OpenStruct params={:meat=>true}> >>helper.do_you_want_meat => ' ,   !' 


3. Secure changes to the console


If you run the console to test something on the production server, there is a danger of accidentally deleting or changing the data in the database. To avoid this, you can use the launch of the console with the option --sandbox

 >>rails r 'puts User.last.name' => 'Fedor' >>rails c >>user = User.last >>user.name = "Petr" >>user.save >>user.name => "Petr" >>exit >>rails r 'puts User.last.name' => 'Fedor' 


4. Removing items from a hash by key


Removes the listed items from the hash and returns the result.

 >>options = {:one => 1, :two => 2, :three => 3, :four => 4} =>{:one => 1, :two => 2, :three => 3, :four => 4} >>options.except!(:one, :two) =>{:three => 3, :four => 4} 


5. Search for a substring in a string


This is usually done using

 >>string = 'corvalol' >>string =~ /lol/ => 5 #       


An alternative way, in my opinion somewhat graceful

 >>string = 'corvalol' >>string['lol'] => 'lol' #   ,   


These are only 5 chips that seemed interesting to me, the links below are much more.

confreaks.com/videos/889-railsconf2012-ten-things-you-didn-t-know-rails-could-do
37signals.com/svn/posts/3176-three-quick-rails-console-tips
rubyreloaded.com/trickshots
stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-rails

Source: https://habr.com/ru/post/146441/


All Articles