Rails Formtastic – How to remove the unordered list from form inputs

When using ‘Formtastic’ the default behavior is to add an unordered list to the set of inputs.  This is specified in its CSS.   This makes the form look like it has a set of radio bullets.

How to remove this?

From an input:

<%=f.input :zip, :label => false, :placeholder => '5 digit zip',
           :input_html => { :class => 'form-control' },
           :wrapper_html => { :style => 'list-style-type:none;' } %>

From a button:

 <%= f.action :submit, :label => "Continue", 
              :button_html => { :class => 'btn btn-danger' },
              :wrapper_html => { :style => 'list-style-type:none;' }%>

 

Advertisements

How to create a guest user in Rails 4

If you have Devise authentication:

 

class ApplicationController < ActionController::Base

  protect_from_forgery

  # if user is logged in, return current_user, else return guest_user
  def current_or_guest_user
    if current_user
      if session[:guest_user_id] && session[:guest_user_id] != current_user.id
        logging_in
        guest_user(with_retry = false).try(:destroy)
        session[:guest_user_id] = nil
      end
      current_user
    else
      guest_user
    end
  end

  # find guest_user object associated with the current session,
  # creating one as needed
  def guest_user(with_retry = true)
    # Cache the value the first time it's gotten.
    @cached_guest_user ||= User.find(session[:guest_user_id] ||= create_guest_user.id)

  rescue ActiveRecord::RecordNotFound # if session[:guest_user_id] invalid
     session[:guest_user_id] = nil
     guest_user if with_retry
  end

  private

  # called (once) when the user logs in, insert any code your application needs
  # to hand off from guest_user to current_user.
  def logging_in
    # For example:
    # guest_comments = guest_user.comments.all
    # guest_comments.each do |comment|
      # comment.user_id = current_user.id
      # comment.save!
    # end
  end

  def create_guest_user
    u = User.create(:name => "guest", :email => "guest_#{Time.now.to_i}#{rand(100)}@example.com")
    u.save!(:validate => false)
    session[:guest_user_id] = u.id
    u
  end

end

 

Redirect to previous page after sign in

When using ‘Devise’ for your authentication mechanism,  you may need to redirect to the previous webpage after signing in.

To do this:

class Users::SessionsController < Devise::SessionsController
  layout 'user'

  after_filter :store_location

  def store_location
    # store last url - this is needed for post-login redirect to whatever the user last visited.
    return unless request.get?
    if request.path != "/users/sign_in" &&
        request.path != "/users/sign_up" &&
        request.path != "/users/password/new" &&
        request.path != "/users/password/edit" &&
        request.path != "/users/confirmation" &&
        request.path != "/users/sign_out" &&
        !request.xhr? # don't store ajax calls
      session[:previous_url] = request.fullpath
    end
  end

  #Take me to the referring page after sign in
  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path
  end

end

 

Displaying Git commit version in Rails

At times, you’re doing rapid development and you’d like to frequently update the GIT commit version that is in use

1.  Create a ‘config/initializers/git_revision.rb’ file and update the following:

module MyProject
     REVISION=`git log --pretty=format:'%h' -n 1`
end

2.  In your _footer.html.erb or equivalent view file, call the following:

Version: <%= MyProject::REVISION %>

This should display the version as follows:

Version: 774318e

Route ‘Sign In’ to the previous page

Ever noticed after doing an elaborate search, your website asks you to do a ‘Sign in’.  After logging in, it forgets all about your search results and takes you back to your home page.

When using Rails with Devise, you may have noticed that ‘signing in’ takes you to the home page of the controller.

In order to go to the last page that redirects you to the ‘devise sign in ‘ page, make the following changes to your Devise ‘Sessions Controller’

#Take me to the referring page after sign in
def after_sign_in_path_for(resource)
   redirect_to request.referrer
end

This should redirect to the ‘referring’ or the ‘calling’ page.

Integrating PaperClip Gem to a Rails Model

Include the gem in ‘Gemfile’

gem "paperclip", "~> 3.0"

1.  Add a rails migration to the model

rails g migration addAvatarColumnToMenu
  invoke  active_record
  create db/migrate/20131113072946_add_avatar_column_to_menu.rb

2.  Edit your migration

 class AddAvatarColumnToMenu < ActiveRecord::Migration
   def self.up
     change_table :menus do |t|
        t.attachment :avatar
     end
   end
   def self.down
      drop_attached_file :menus, :avatar
   end
 end

3.  Add the following to your model

class Menu < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end

4.  Run the migration

 rake db:migrate
==  AddAvatarColumnToMenu: migrating ============================
-- change_table(:menus)
-> 0.5326s
==  AddAvatarColumnToMenu: migrated (0.5328s) ===================

5.  You should see the 4 columns added if you run “rails console”

avatar_file_name: string, avatar_content_type: string, avatar_file_size: integer, avatar_updated_at: datetime

6.  Add this to your “new” and “update” views

<%= form_for @menu, :url => menus_path, :html => { :multipart => true } do |form| %>
  <%= form.file_field :avatar %>
<% end %>

7.  Add this to the ‘show’ view

<%= image_tag @menu.avatar.url %>
<%= image_tag @menu.avatar.url(:medium) %>
<%= image_tag @menu.avatar.url(:thumb) %>

8.  Restart the server to get the new table data set.

Integrating PaperClip to a Rails Model

Include the gem in ‘Gemfile’

gem "paperclip", "~> 3.0"

1.  Add a rails migration to the model

      rails g migration addAvatarColumnToMenu
      invoke  active_record
      create db/migrate/20131113072946_add_avatar_column_to_menu.rb

2.  Edit your migration

 class AddAvatarColumnToMenu < ActiveRecord::Migration
   def self.up
     change_table :menus do |t|
        t.attachment :avatar
     end
   end
   def self.down
      drop_attached_file :menus, :avatar
   end
 end

3.  Add the following to your model

class Menu < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end

4.  Run the migration

 rake db:migrate
==  AddAvatarColumnToMenu: migrating ============================
-- change_table(:menus)
-> 0.5326s
==  AddAvatarColumnToMenu: migrated (0.5328s) ===================

5.  You should see the 4 columns added if you run “rails console”

avatar_file_name: string, avatar_content_type: string, avatar_file_size: integer, avatar_updated_at: datetime

6.  Add this to your “new” and “update” views

<%= form_for @menu, :url => menus_path, :html => { :multipart => true } do |form| %>
  <%= form.file_field :avatar %>
<% end %>

7.  Add this to the ‘show’ view

<%= image_tag @menu.avatar.url %>
<%= image_tag @menu.avatar.url(:medium) %>
<%= image_tag @menu.avatar.url(:thumb) %>

8.  Restart the server to get the new table data set.