Skip to content

Instantly share code, notes, and snippets.

@stevecass
Created January 17, 2016 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevecass/c6b4651a17f07ec78212 to your computer and use it in GitHub Desktop.
Save stevecass/c6b4651a17f07ec78212 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 (to allow 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 #{key}" %>
  <% end %>
</div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment