How to configure sequence separator of friendly_id gem - ruby-on-rails-3

I am using the friendly_id gem version 4.0.9 in Rails 3, and I want to change the default sequence separator from - to _.
I tried this but it doesn't work:
class Restaurant < ActiveRecord::Base
extend FriendlyId
friendly_id do |config|
config.base = :name
config.use :slugged
config.sequence_separator = "_"
end
.....
end

Taken from the gem's tests on GitHub:
friendly_id :name, :use => :slugged, :sequence_separator => "_"
Should work...

For Friendly_Id Gem 5.x with Rails 4.x
(Might also work with lower versions of Friendly_Id Gem or Rails)
Add the following method to the model file where you are using Friendly_Id to generate slugs-
def normalize_friendly_id(string)
super.gsub("-", "_")
end

Related

Devise : undefined method `user_signed_in? Didn't know why?

Please help i am using rails 3.2.11 and devise 2.0.6 but when i am trying to run my project i am getting an error undefined method `user_signed_in?' for Class
i have done lot of google but didnt find any solution.!
Below is my application trace
NoMethodError in Home#index
Showing /home/amits/RailsWorkspace/Myapp/app/views/home/index.html.haml where line #3 raised:
undefined method `user_signed_in?' for #<#<Class:0xbb1f3f4>:0xbae60b8>
Extracted source (around line #3):
1: %h2= (t :welcome) + " Myapp"
2:
3: - if user_signed_in?
4: = link_to (I18n.t :customers), user_root_path
5: / %li
6: / = link_to (I18n.t :sign_out), destroy_user_session_path, :method => :delete
Rails.root: /home/amits/RailsWorkspace/Myapp
Application Trace | Framework Trace | Full Trace
app/views/home/index.html.haml:3:in `_app_views_home_index_html_haml___806580830_97751900'
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
and my gem file is as follows
Gemfile :
source 'https://rubygems.org'
gem 'rails', '3.2.11'
gem 'heroku'
gem 'pg'
gem 'haml'
gem 'haml-rails', :group => :development
gem 'rmagick'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.0'
gem 'uglifier', '>= 1.0.3'
end
gem "devise", "~> 2.0.0"
gem 'devise_aes_encryptable'
# version added so that the gem version will be stable at this point.
gem 'geokit', '1.5.0'
gem 'geokit-rails3', '0.1.5'
My Home controller
Homescontroller.rb
class HomeController < ApplicationController
before_filter :authenticate_user!
def index
end
end
My user model
User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :sign_in_count
end
Rotes.rb
Myapp::Application.routes.draw do
devise_for :users
root to: "home#index"
end
Please help me....
Try removing 'clear_helpers' method in ApplicationController as it might block sometimes Devise to load his helpers.

CarrierWave not working with Fog -> ArgumentError ( is not a recognized storage provider): app/controllers/notes_controller.rb:11:in `create'

I followed the railscasts about using carrierwave with fog with aws s3 to the T but it doesnt seem to work.
The Gemfile is as follows:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'pg_search'
gem 'devise'
gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails", :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
gem "simple_form"
gem 'aws-sdk', '~> 1.8.1.2'
gem 'activerecord-reputation-system', require: 'reputation_system'
gem 'bootstrap-will_paginate'
gem 'carrierwave'
gem "rmagick"
gem "fog", "~> 1.3.1"
gem 'carrierwave-aws'
config/initializers/carrierwave.rb
require 'carrierwave'
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV["xxx"],
aws_secret_access_key: ENV["yyy"],
region: ENV['ap-southeast-1']
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV["ABC"]
end
The uploader:
class ImageUploader < CarrierWave::Uploader::Base
require 'carrierwave/processing/mime_types'
before :store, :remember_cache_id
after :store, :delete_tmp_dir
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_limit => [200, 200]
end
# store! nil's the cache_id after it finishes so we need to remember it for deletion
def remember_cache_id(new_file)
#cache_id_was = cache_id
end
def delete_tmp_dir(new_file)
# make sure we don't delete other things accidentally by checking the name pattern
if #cache_id_was.present? && #cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
FileUtils.rm_rf(File.join(root, cache_dir, #cache_id_was))
end
end
end
the controller:
class NotesController < ApplicationController
before_filter :authenticate_user!, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def index
end
def create
#note = current_user.notes.build(params[:note])
#notes = Note.paginate(page: params[:page])
if #note.save
flash[:success] = "Note uploaded!"
redirect_to root_url
else
flash.now[:error] = "Note failed to upload."
render 'static_pages/home'
end
end
def destroy
#note.destroy
flash[:success] = "Note deleted."
redirect_to root_url
end
private
def correct_user
#note = current_user.notes.find_by_id(params[:id])
redirect_to root_url if #note.nil?
end
end
I don't really know if the problem lies in the gem or the controller. Even the controller is based on the microposts controller in Michael Hartl's book "The Ruby on Rails Tutorial" so I don't know where I have gone wrong.
That's an awfully early version of fog. The latest is 1.12.1. Try updating to the latest release using the following entry in your Gemfile
gem "fog", "~> 1.12.1"

Recaptcha with devise is not working on ruby on rails?

I get this error:
There has been an unexpected error with the application. Please contact the administrator. error code: success
I am using recaptcha with devise, I edited where appropriate as stated by following the steps mentioned on this page
Here is my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'json'
gem 'execjs'
gem 'therubyracer'
gem 'will_paginate', "3.0.pre4"
group :production do
gem 'pg'
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
gem 'jquery-fileupload-rails'
gem "letter_opener"
gem 'jquery-ui-rails'
end
gem 'jquery-rails'
gem "carrierwave"
gem "fastercsv"
gem 'devise', '2.1.2'
gem 'rmagick'
gem 'mailman', :require=> false
gem 'activeadmin'
gem 'kaminari'
gem 'prawn'
gem 'recaptcha', :require => 'recaptcha/rails'
I hope you have followed it exactly what it says on the documentation For instance, to support recaptcha on ruby 1.8.X and 1.9.X, there are different line of code. However, after a short research I found out this website you can use recaptcha without the plugins regardless of your rails version. I hope this helps. :)
From Recaptcha Site (almost).... I'm in Rails 4 with Devise (aparently the hard bundle).. No, is too easy. Procedure apply to any ruby, any rails, and devise (or without devise). Tested in ruby/rails/devise 1.9.3/3.2.x/2.2.1 & 2.0.x/4.0.x/3.2.4 and last one without Devise.
Edit next files:
Gemfile
gem 'ruby-recaptcha'
# bundle install
config/initializers/credentials.rb (any file in this path it's ok)
# Set constants
RCC_PUB = "your_public_key"
RCC_PRIV= "your_private_key"
# re-start server. Try to keep safety this data using ENV vars.
app/views/devise/registrations/new.html.erb (or form you prefer)
# form
<%= simple_form_for(resource, as: resource_name, :url => registration_path(resource_name) do |f| %>
<%= f.input :email, autofocus: true, required: true %>
...
<!-- Add next line -->
<div id="captchadiv"></div>
<%= f.button :submit, "Register", class: 'btn btn-danger' %>
<% end %
At the end of same file (can be in awesome_funcs.js.erb file)
<script type="text/javascript" charset="utf-8">
$(function () {
function showRecaptcha() {
Recaptcha.create("<%= RCC_PUB %>", "captchadiv", {
theme: "white",
lang: 'en'
});
};
showRecaptcha();
});
</script>
And the little trick, app/controllers/application_controller.rb
# On top
include ReCaptcha::AppHelper
before_action :configure_permitted_parameters, if: :devise_controller?
# If already you have this method or any other with before_filter
# Into method
protected
def configure_permitted_parameters
if controller_name == "registrations" && action_name == "create"
# Rails 3: If you prefer, put this into registrations#create method
unless validate_recap(params, User.new.errors) # Or your model
# wrong input is handled here
flash[:error] = "Text from images doesn't match with your input"
redirect_to :back # or other stuff
# As my submit is with Ajax, I do a render instead redirect:
# render :json => {other: "recaptcha_failed"}
# In success callback I evaluate response and Recaptcha.reload() paints a new recaptcha.
end
end
....
# trick: do everything before params sanitizers
end
As we are using Ajax plugin, finally, add in
app/views/layouts/application.html.erb
(just before end of body)
<body>
...
...
<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
</body>
That's it. You have recaptcha
Advice:
If you use Ajax to do submit, you can use success callback for a Recaptcha.reload() in case images and input doesn't match.
Other, if your form is "remote: true", you can use above behavior after "create" method in create.js.erb file in your views directory.
IMPORTANT: In some projects when form arrives from a link Recaptcha doesn't appear. Try to locate the launcher link into a div or in a parent div add "data-no-turbolink". I had this issue in Rails 4.

Formtastic ~> 2.0.2 and enumerated_attribute gem, Rails 3.1.1

I used enumerated_attribute with formtastic ~> 1.2.3 with "monkey patch" for field :as => :enum and everything worked fine.
But when I updated formtastic to 2.0.2 version appeared an error with message "Formtastic::UnknownInputError".
For more details here is patch, that was added to /initialisers/formtastic.rb:
module Formtastic #:nodoc:
class SemanticFormBuilder #:nodoc:
def enum_input(method, options)
unless options[:collection]
enum = #object.enums(method.to_sym)
choices = enum ? enum.select_options : []
options[:collection] = choices
end
if (value = #object.__send__(method.to_sym))
options[:selected] ||= value.to_s
else
options[:include_blank] ||= true
end
select_input(method, options)
end
end
end
P.S. I tried to change SemanticFormBuilder to FormBuilder (as I understood from new formtastic documentation there was such change for all custom inputs), but I was still getting error
Maybe anybody already used these gems together successfully?
They way custom fields are defined has changed completely in Formtastic 2.x
You need to subclass the internal Formtastic classes to get what you want. A select input would look something like this:
module FormtasticExtensions
class EnumeratedInput < Formtastic::Inputs::SelectInput
def collection
# programmatically build an array of options in here and return them
# they should be in this format:
# [['name', 'value'],['name2', 'value2']]
end
end
end
Include the module in the Formtastic initializer:
include FormtasticExtensions
and this will give you a field :as => :enumerated and you should be good to go. In my case (some other custom field) it selects the current option, but you may need to tweak the code for yours to work.
You could also just pass the collection in:
f.input :thing, :as => :select, :collection => your_collection, :label_method => :your_name, :value_method => :your_id

undefined method `within'

I followed the installation procedure of geokit-rails3, here is my conf :
Using rails (3.0.4)
Using activerecord (3.0.4)
Using geokit (1.5.0)
Using geokit-rails (1.1.4)
I get this error "undefined method `within' for #" when i try to query with the "within" method. (note that i am new to rails and maybe missing something obvious)
Here is my class definition :
class Snip < ActiveRecord::Base
belongs_to :user
acts_as_mappable :default_units => :kms,
:default_formula => :sphere,
:distance_field_name => :distance,
:lat_column_name => :latitude,
:lng_column_name => :longitude
end
In my controller i have :
#userLocation = GeoKit::LatLng.new(params[:lat],params[:lng])
#snips = Snip.within(params[:distance], :origin => #userLocation)
Here is what i added to my gemfile :
gem 'geokit', '>= 1.5.0'
gem 'geokit-rails', '1.1.4'
Do you have any idea why i get this error ?
Thanks in advance,
Vincent.
Your gemfile asks for geokit-rails, that's the Rails 2 version. You need to replace that with geokit-rails3