Skip to content

Instantly share code, notes, and snippets.

@stevecass
Created April 9, 2016 11:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevecass/2febe9bcb70e82ec163a91164a408936 to your computer and use it in GitHub Desktop.
Save stevecass/2febe9bcb70e82ec163a91164a408936 to your computer and use it in GitHub Desktop.

Moving to Rails from Sinatra

Helpers

Helper methods in rails are used only in views, not controllers. If you want to make a method available to multiple controllers, you can declare it in the ApplicationController, which is the base class for all your controllers. If you want the method to be visible to views, declare it as a helper_method.

http://api.rubyonrails.org/classes/AbstractController/Helpers/ClassMethods.html#method-i-helper_method

As noted in that link, the application_controller is a good place for your logged_in? and current_user methods.

True helper methods are generally concerned only with the view. So you might make a helper that can display particular output depending on the state of a model you give it as an argument, or a helper that formats dates. Your view helpers may look like they are in separate modules, but you should be aware that in view rendering, all your helper methods are placed in the same global namespace, so be careful the names of your helper methods don't clash. In general, you will write few helpers for the work you do at DBC.

Forms, links and buttons

Forms and links are best constructed using form_for, link_to and friends, rather than writing your own HTML.

form_for - best for forms for creating and editing ActiveModel or ActiveRecord objects.

form_tag - used for forms not bound to a model object (e.g. a login form or maybe a search form.)

link_to - create a link. So link_to 'home', root_path will generate <a href="/">home</a> - these become more useful as paths get more complex.

button_to - creates a form containing a button to submit the form. The button action is set to the url passed in. So button_to 'Delete' post_path(post), method: :delete might generate

<form class="button_to" method="post" action="/posts/1">
<input type="hidden" name="_method" value="delete" />
<input type="submit" value="Delete" />
<input type="hidden" name="authenticity_token" value="..." />
</form>

The Flash

http://api.rubyonrails.org/classes/ActionDispatch/Flash.html http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html

The Rails flash is an inbuilt mechanism that allows you to pass temporary values to the next action in the current session. For example on successful login you might do

session[:user_id] = user.id
flash[:notice] 'Welcome to our site'
redirect_to root_path

When the browser makes the request to '/' in response to the redirect, the flash is accessible to that action. So you might have in your applciation layout something like

<% if flash.any? %>
<div id="flash-container">
  <% flash.each do |key, value| %>
    <%= content_tag :div, value, class: "flash flash-#{key}" %>
  <% end %>
</div>
<% end %>

The flash is an extended hash. The usual pattern is to use key :notice for success messages, and :alert for error and warning messages. In that case, if you were using the layout snippet above, each message would be rendered on one of div class="flash flash-notice" or div class="flash flash-alert" You might then choose to use CSS to have those divs flash or display in a pop-up or use some other way to draw the user's attention to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment