how to use auto_complete with rails3? - ruby-on-rails-3

After searching on the web I got a problem :
I use rails3 and I do the following command
rails plugin install git://github.com/patshaughnessy/auto_complete.git
then i restart my rails server
my index.html.erb :
<%= text_field_with_auto_complete :departement, :nom, {}, {:method => :get} %>
my controller :
class AutocompController < ApplicationController
auto_complete_for :departement, :nom
end
I got a model like :
class CreateDepartements < ActiveRecord::Migration
def self.up
create_table :departements do |t|
t.column :region_id, :integer
t.column :num_dept, :string
t.column :nom, :string
end
end
def self.down
drop_table :departements
end
end
my routes.rb :
resources :autocomp, :collection => { :auto_complete_for_departement_nom => :get }
I get the following error :
No route matches {:controller=>"autocomp", :action=>"auto_complete_for_departement_nom"}
I don't get why it doesn't work? is auto_complete compatible with rails3? Or should I use jQuery?

You need to setup the route. Its missing, therefor you get this message.
Goto config/routes.rb and add your route, like:
resources : departements do
get : auto_complete_for_departement_nom, :on => :collection
end
Im using, autocomplete

Related

Heroku Production Rails(3.2.3) Active admin(0.5) Globalise 3 NoMethodError in create & update

As am working on the Active Admin(0.5) with Rails 3.2.3 Using Multilingual Support using Globalise 3 .
When am running application on local under development & production environment everything works great.
But when i deployed it on to the heroku production mode it throwing me error with the same code am using over local.
When i click on create new page it giving me below error:
NoMethodError in Admin::HomeTemplatesController#new
undefined method `build_app_page' for nil:NilClass
Rails.root: /app
Application Trace | Framework Trace | Full Trace
app/admin/home_templates.rb:16:in `new'
Please find below the code
ActiveAdmin.register HomeTemplate do
menu false
config.clear_action_items!
form :partial => "form"
controller do
protect_from_forgery :except => :sort
def new
# #home_template =HomeTemplate.new
# if !!current_ability.attributes_for(:create, HomeTemplate)[:app_instance_id]
# #valid_parents = AppPage.where("app_instance_id = ? AND parent_id IS NULL", current_ability.attributes_for(:create, HomeTemplate)[:app_instance_id])
# end
#home_template.build_app_page
end
def create
# if !!current_ability.attributes_for(:create, HomeTemplate)[:app_instance_id]
# #valid_parents = AppPage.where("app_instance_id = ? AND parent_id IS NULL", current_ability.attributes_for(:create, HomeTemplate)[:app_instance_id])
# end
if #home_template.present?
#home_template.app_page.protected = false;
#home_template.app_page.hidden = false;
#home_template.app_page.app_instance = #home_template.app_instance;
create!
end
end
def update
##home_template = HomeTemplate.find(params[:id])
#valid_parents = AppPage.where("app_instance_id = ? AND parent_id IS NULL", #home_template.app_page.app_instance_id)
update!
end
def edit
# if !!current_ability.attributes_for(:edit, HomeTemplate)[:app_instance_id]
# #valid_parents = AppPage.where("app_instance_id = ? AND parent_id IS NULL", current_ability.attributes_for(:edit, HomeTemplate)[:app_instance_id])
# Rails.logger.info( #valid_parents)
# Rails.logger.info( current_ability.attributes_for(:edit, HomeTemplate)[:app_instance_id])
# end
Rails.logger.info( "Home template outside")
##home_template = HomeTemplate.find(params[:id])
#valid_parents = AppPage.where("app_instance_id = ? AND parent_id IS NULL", #home_template.app_page.app_instance_id)
Rails.logger.info(#valid_parents)
end
end
collection_action :sort, :method => :post do
if(params[:ids])
params[:ids].each_with_index do |id, index|
app_page = AppPage.find(id)
app_page.move_to_child_of app_page.parent_id if can? :update, app_page.templatable
end
end
head 200
end
end
I tried to connect with console through heroku & it fetching the models.
I also checked the postgres database over local in production ode & all worked like a charm.
Right now am using Sqlite & postgres over local & heroku as postgres.
Your Help & guidance will be highly appreciable & deserves a hat off from my side.
Please let me know if any more details required.
Right now am using Polymorphic Association
as below
class HomeTemplate < ActiveRecord::Base
include BelongsToAppInstance
has_one :app_page, :as => :templatable
has_many :home_banner_images, :dependent => :destroy
accepts_nested_attributes_for :home_banner_images, :allow_destroy => true
accepts_nested_attributes_for :app_page
validates_presence_of :app_page
end
AppPages as:
class AppPage < ActiveRecord::Base
include BelongsToAppInstance
belongs_to :last_modified_by, :class_name => "AdminUser"
validates_presence_of :last_modified_by
belongs_to :templatable, :polymorphic => true, :dependent => :destroy
has_many :app_page_callouts
acts_as_nested_set
attr_readonly :hidden
attr_readonly :protected
default_scope :order => 'lft ASC'
validates_presence_of :name
#validates_uniqueness_of :name, :scope => :app_instance_id
validates_uniqueness_of :app_page_role, :scope => :app_instance_id
mount_uploader :related_pdf, PdfUploader
validate :parent_must_belong_to_same_app_instance
#validates_inclusion_of :linked_model, :in => AppPage.allowable_linked_models, :unless => Proc.new {|app_page| app_page.linked_model.blank? }
attr_accessible :name,:subtitle,:body,:app_page_attributes,
:app_instance_id,:app_page_role,:related_pdf,
:parent_id,:translations_attributes,
:last_modified_by,:app_instance,:sort,:view_controller,:hidden,:protected,:app_page_callouts,:parent
active_admin_translates :name,:subtitle,:body
#validates_globalized_uniqueness_of :name,:scope => :locale
def parent_must_belong_to_same_app_instance
if !self.parent_id.nil? && self.parent.app_instance_id != self.app_instance_id
errors.add(:parent_id, "The parent must belong to the same app instance.")
end
end
def set_view_controller
if self.view_controller.nil?
self.view_controller = "detail"
end
end
end
Actually am using active admin & in this we don't define #home_tamplate anywhere but in local or dev phase its working fine but when i uploaded it to heroku its start throwing error. Sometime when i restart the heroku it worked but other controller stop working.Its occurring very randomly over heroku with all controllers. I introduced the new & update statement & when i on this it worked for new but when i submit to create it again i get the home_template nil.......

Reserved word issue with ActiveAdmin rails 3.2

I have made the following addition to my active admin interface:
action_item :only => :show do
link_to('Approve this article', approve_admin_article_path(article)) if article.approved.nil?
end
member_action :approve, :method => :get do
# do approval
redirect_to :action => :show, :notice => "Approved!"
end
This throws the following error:
undefined method `approved' for
:Arbre::HTML::Article
What I think is happening is Active Admin thinks I'm passing an article tag in, not an article class?
Does anyone know of a work around for this? perhaps aliasing?
Thanks!
class Article < ActiveRecord::Base
attr_accessible :body
# Relations:
belongs_to :articleable, polymorphic: true, :counter_cache => true
has_many :comments, as: :commentable, order: 'created_at DESC', dependent: :destroy
# Validations
validates_presence_of :body
validates_length_of :body, maximum: 15000
end
Found a workaround
There is something fishy when you name your class as 'Article', ActiveAdmin relate to it when rendering as <article> HTML tag - The problem is somewhere in the controller of course because this is where the article object is being generated
So, I override the controller
ActiveAdmin.register Article do
controller do
def show
# grabbing my desired Article and not the <article> tag into some global variable
##myarticle = Article.find(params[:id])
end
end
sidebar :article_details , :only => :show do
ul do
# using the ##myarticle which I know should be initialized
# (you can put .nil? checking here if you want)
li link_to 'Article Images (' + ##myarticle.images.count.to_s + ')' , admin_article_article_images_path(##myarticle)
li link_to 'Article Clips ('+##myarticle.clips.count.to_s + ')' , admin_article_article_clips_path(##myarticle)
end
end
end
Enjoy
Assuming you're having the issue in the 'show' block, you could change the show block to the following:
show do |object|
end
Then you can call object.some_method without the clash. This way you don't need to override the controller.

Rails 3: Problem with a route to new action

I have a problem with rails 3 routes.
I want add a new action called "gestion_etudiant" with a new view "gestion_etudiant.html.erb".
I have on my index page a link like this
<%= link_to "Administration", {:controller => "users", :action => "gestion_etudiant"} %>
I also try this:
<%= link_to "Administration", "/users/gestion_etudiant" %>
In my controller:
def gestion_etudiant
#users = User.find(:all)
end
but when I clic on the link, I always have this error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=gestion_etudiant
I have this in my routes file:
resources :users
And I've also try to add:
match "users/gestion_etudiant", :to => "users#gestion_etudiant"
and
resources :users, :only => [:gestion_etudiant]
But I can not access my page "gestion_etudiant.html.erb". Can anybody suggest why?
Try this:
# router:
resources :users do
get :gestion_etudiant, :on => :collection
end
# view:
link_to "Administration", gestion_etudiant_users_path
# Controller
def gestion_etudiant
#users = User.all # Don't use find with :all as it will be deprecated in rails 3.1
end
In your routes, try:
resources :users do
collection do
get 'gestion_etudiant'
end
end
You can check the routes you have in your application by running rake routes

How do I enable :confirmable in Devise?

The newest version of Devise doesn't have :confirmable enabled by default. I already added the respective columns to the User model but cannot find any code examples of how to enable :confirmable.
Where can I find a good example or what code do I need to enable it?
to "enable" confirmable, you just need to add it to your model, e.g.:
class User
# ...
devise :confirmable , ....
# ...
end
after that, you'll have to create and run a migration which adds the required columns to your model:
# rails g migration add_confirmable_to_devise
class AddConfirmableToDevise < ActiveRecord::Migration
def self.up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at , :datetime
add_column :users, :unconfirmed_email, :string
add_index :users, :confirmation_token, :unique => true
end
def self.down
remove_index :users, :confirmation_token
remove_column :users, :unconfirmed_email
remove_column :users, :confirmation_sent_at
remove_column :users, :confirmed_at
remove_column :users, :confirmation_token
end
end
see:
Adding confirmable module to an existing site using Devise
I'd recommend to check the source code to see how Confirmable works:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
You could also check the RailsCast on Devise:
http://railscasts.com/episodes/209-introducing-devise
Next it would be best to search for example applications on GitHub
This question seems to be odd one ;-) If you written some migration alike:
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
and as you said little change in model (passing additional => :confirmable to devise) like so:
devise :database_authenticatable, :registerable, :confirmable
you can now generate some views (if you didn')
rails generate devise:views
You can go to app/views/devise/confirmations/new.html.erb and check how it looks like or change it. Furthermore you can inspect app/views/devise/confirmations/shared/_links.erb => there is line:
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
This condition checks if confirmable is turned on so... technically if everything went fine it should works OOTB. After creating new account - in log - you should see lines where confirmation mail is sent with appropriate link. It triggers:
Rendered devise/mailer/confirmation_instructions.html.erb
so you have got next place where you can customize it a bit
How to customize confirmation strategy? Please ask exact question what do you want to achieve. You can check devise gem path. In /lib/devise/models/confirmable.rb some comments could be helpful.
regards
If you've already installed devise into your app, and want to add "confirmable" later, instead of running:
rails generate devise:views
as mentioned by Piotr, run
rails generate devise:views confirmable
to produce only the views needed for "confirmable". You'll see output like this:
rails generate devise:views confirmable
invoke Devise::Generators::SharedViewsGenerator
create app/views/confirmable/mailer
create app/views/confirmable/mailer/confirmation_instructions.html.erb
create app/views/confirmable/mailer/reset_password_instructions.html.erb
create app/views/confirmable/mailer/unlock_instructions.html.erb
create app/views/confirmable/shared
create app/views/confirmable/shared/_links.erb
invoke form_for
create app/views/confirmable/confirmations
create app/views/confirmable/confirmations/new.html.erb
create app/views/confirmable/passwords
create app/views/confirmable/passwords/edit.html.erb
create app/views/confirmable/passwords/new.html.erb
create app/views/confirmable/registrations
create app/views/confirmable/registrations/edit.html.erb
create app/views/confirmable/registrations/new.html.erb
create app/views/confirmable/sessions
create app/views/confirmable/sessions/new.html.erb
create app/views/confirmable/unlocks
create app/views/confirmable/unlocks/new.html.erb
You'll then be able to access these files directly in your project to style them like your application. You'll also be able to change the messaging in the emails Devise sends out through the generated mailer views.
Last, don't forget to add config.action_mailer.delivery_method and config.action_mailer.smtp_settings in your app/config/environments/{environment_name}.rb file. This is what my production.rb file looks like:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => '[redacted]',
:user_name => '[redacted]',
:password => '[redacted]',
:authentication => 'plain',
:enable_starttls_auto => true }
Checkout devise wiki page. There is a full answer for your question.
For DRY, you can also put mailer config in config/initializers/mail.rb like:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => '[redacted]',
:user_name => '[redacted]',
:password => '[redacted]',
:authentication => 'plain',
:enable_starttls_auto => true }
After configuring the ActionMailer setting described above I had to make one last addition to the config/environments/development.rb file to fix an error page that would show up after registering a new user:
config.action_mailer.default_url_options = { :host => 'localhost' }
More details about this solution: Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]

Unable to display associated model's fields

Hey all,
I'm having a pretty weird problem and am having trouble isolating the cause. I get the feeling its a simple solution, but I was hoping for a nudge in the right direction. I have the following models:
class User < ActiveRecord::Base
has_many :pnotes
class Pnote < ActiveRecord::Base
belongs_to :preport
belongs_to :user
class Preport < ActiveRecord::Base
has_many :pnotes, :dependent=>:destroy
The Pnote portion of my schema looks as so:
create_table "pnotes", :force => true do |t|
t.integer "preport_id"
t.integer "user_id"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
I'm trying to display all of the Pnotes associate with a given Preport on the show page of the Preport
My Preport Controller has the following code for the show action:
def show
#board = Board.find(params[:board_id])
#preport = #board.preport
#pnotes = #preport.pnotes
#pnote=#preport.pnotes.build
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #preport }
end
end
So now in my show.html.erb in the Preports folder, I am trying to display a list of the Pnotes as such:
<ul>
<% #pnotes.each do |pnote| %>
<li><%= pnote.user.login %><span>-<%=pnote.content %></span></li>
<% end %>
</ul>
"login" is the standard field that came along with the Devise Authentication Gem.
However, this gives me the following error:
undefined method `login' for nil:NilClass
If I change the view to this:
<%= pnote.user%>-<%=pnote.content %>
I get :
#<User:0x106c25888>-Testing
, so it appears that it is identifying the associated user, but unable to grab its attributes.
Any advice would be greatly appreciated! I feel like there is a fairly small oversight going on here, but I am unable to distinguish from exactly where
Thanks!
Unless you're using a very old version of Devise that I'm not aware of, the User model wouldn't have a login attribute.
Try email instead.