Skip to content

Instantly share code, notes, and snippets.

@rocLv
Created December 30, 2014 03:16
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 rocLv/2beaceccd5409262a2f3 to your computer and use it in GitHub Desktop.
Save rocLv/2beaceccd5409262a2f3 to your computer and use it in GitHub Desktop.
link_to
image_tag
link_to image_tag(“rails.png”, alt: “Rails logo”), “#”
<%= debug(params) if Rails.env.development %>
Because we used theimage_tag helper in Listing 5.2, Rails will automatically find any images in theapp/assets/images/ directory using the asset pipeline (Section 5.2).
The directory
app/assets/stylesheets/
is part of the asset pipeline (Section 5.2), and any stylesheets in this directory will automatically be included as part of the application.css file included in the site layout.
When creating a column in a database, it is important to consider whether we will need tofind records by that column.
In Listing 6.31, we could have written the assignment as
self.email = self.email.downcase
(where self refers to the current user), but inside the User model the self keyword is optional on the right-hand side:
self.email = email.downcase
Add bootstrap css to stylesheet:
@import "bootstrap-sprockets";
@import "bootstrap";
@rocLv
Copy link
Author

rocLv commented Dec 30, 2014

methods defined in any helper file are automatically available in any view,

@rocLv
Copy link
Author

rocLv commented Dec 30, 2014

As noted in the Gravatar documentation, Gravatar URLs are based on an MD5 hash of the user’s email address. In Ruby, the MD5 hashing algorithm is implemented using the hexdigest method, which is part of the Digest library:

email = "rocwar@gmail.com".
Digest::MD5::hexdigest(email.downcase)
=> "a0dd2a6e2799d405b45dd828bb6d9731"

@rocLv
Copy link
Author

rocLv commented Dec 30, 2014

Rails automatically wraps * the fields with errors * in divs with the CSS class * field_with_errors *. These labels then allow us to style the error messages with the SCSS shown in Listing 7.20, which makes use of Sass’s @extend function to include the functionality of the Bootstrap class has-error.

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

Other errors methods:

user.errors.count
user.errors.empty?
user.errors.any?

@rocLv
Copy link
Author

rocLv commented Dec 30, 2014

pluralize(1,"error")

@rocLv
Copy link
Author

rocLv commented Dec 30, 2014

Conveniently, a Sessions helper module was generated automatically when generating the Sessions controller (Section 8.1.1). Moreover, such helpers are automatically included in Rails views; by including the module into the base class of all controllers (the Application controller), we arrange to make them available in our controllers as well (Listing 8.11).

 #app/controllers/application_controller.rb
 class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end

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