undefined method `friendly' getting error from controller - ruby-on-rails

I have work on project with ruby on rails and i want to use friendly_id gem. When I install friendly_id gem in my project then add following code according to instructions.
controller:
def show
#group = Group.friendly.find(params[:id])
end
Model:
extend FriendlyId
friendly_id :title, use: [:slugged]
I am using rails version 3.2.11 and friendly_id gem is 4.0.0
Getting error in controller. i do not understand why its happening.
error:
undefined method `slug' for #<Group:0x000000092ac228>
Update:
Previous issue solve with restart server and migrating database.
but it generates new issue Couldn't find group with id=new-demo-group

It seems like you need to update the records that are already created, in your case that would be (Group model), as docs says:
If you're adding FriendlyId to an existing app and need to generate
slugs for existing users, do this from the console, runner, or add a
Rake task:
Group.find_each(&:save)
Rails console
Open terminal on the app root and run (-e is the environment)
rails console -e production
You should get a similar output to this (I added the command to run):
Running via Spring preloader in process 5389
Loading production environment (Rails 5.1.5)
2.5.1 :001 > Group.find_each(&:save)
Rails runner
Open terminal on the app root and run (-e is the environment)
rails runner -e production "Group.find_each(&:save)"

Related

Spring is troubling rails console to start

When i am trying to execute:
bundle exec rails console
It is throwing me error as:
/gems/spring-3.1.1/lib/spring/application.rb:96:in `preload': Spring only supports Rails >= 5.2.0 (RuntimeError)
Earlier it was working fine.
Can anyone help me with a workaround for this.
The workaround I did for this was to create a new project with rails 5.2.3 or something.
If you want to do the same, you can first list all your local gems by doing gem list rails --local
You will see all your rails versions installed locally.
Then do gem install rails -v '5.2.3'
Now go to a new project directory and run rails _5.2.3_ new appname
Now, you should be able to use the console, generate, migrate and other commands.
You can do bundle install after adding other relevant gems based on your apps requirement.

Gem seems to affect other rails project of the same name

I created a rails project MyProject and tried to set up Devise in it (using this tutorial). After I did some Devise stuff I could not scaffold User anymore, I ran into an error as shown below. So I wanted to start over, renamed the project to MyProject-Devise and created a new project MyProject to create User first and then do my Devise stuff. Now in my new project of the same name, when I run
rails generate scaffold user name:string email:string
I run into the same error. If I create a new app with another name, I can scaffold the user with the same command. So I guess it must be the same name thing.
Any ideas why this happens? How to get rid of the error or how to properly start the project over again?
This is the error:
/Users/luke/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/inflector/methods.rb:238:in `const_get': uninitialized constant User (NameError)
Full error here: http://pastebin.com/KFh4U6aS
It looks like you might not be using Bundler. The tutorial you linked to had you create a Gemfile but didn't tell you to run bundle or bundle exec. Without Bundler you may be using a Devise installed directly into your Ruby's gems.
I suspect if you run the rails generate command with bundle exec you may have different results.
bundle exec rails generate scaffold user name:string email:string

Model from Gem is missing

I am using mailboxer in my Rails 4 app.
gem 'mailboxer'
I recently updated it from 0.11 to 0.12.4, it stopped working, and I cant figure out why. Now, I am getting the error:
uninitialized constant Message
I checked the gem's GitHub repo and it does have the Message model.
I checked my local version of the gem and it matches up.
Puma:mailboxer Jeff$ pwd
/Users/Jeff/.rvm/gems/ruby-2.0.0-p247/gems/mailboxer-0.12.4/app/models/mailboxer
Puma:mailboxer Jeff$ ls
conversation conversation.rb mailbox.rb message.rb notification.rb receipt.rb
Why isn't my app finding the model? How do I fix this?
Message is now namespaced as Mailboxer::Message. According to the upgrade documentation, you also need to run through a few steps when upgrading from 0.11 to 0.12:
rails generate mailboxer:namespacing_compatibility
rails generate mailboxer:install -s
rake db:migrate

Rails : Cannot include PgSearch Module provided by the pg_search Gem

TO SUM UP :
The module PgSearch provided by the Gem pg_search cannot be included, required or loaded on the staging environment (Rbenv, nginx, unicorn,capistrano), the problem happens on the web server through http but does not appear on the staging server's rails command.
An other module provided by an other gem can be included without error.
No problem on the local development environment (rvm, puma).
DETAILS
I am currently developing a Ruby On Rails 4.0 application with ruby 2.0.0 which git repositories are hosted on bitbucket.
I deploy the app through a staging server using capistrano.
Staging server environment : rbenv, nginx and unicorn
Local development environment : rvm and puma
The rails environnment files (environment/production.rb & environment/staging.rb) for both are the same.
WHAT DID I DO :
I have installed the pg_search gem (a PostgreSQL full text search gem) by adding it to my Gemfile and put the clause "include PgSearch" in the Active Record model I wanted to use with pg_search gem
I have run the app in development mode... it works !
PROBLEM :
After having deployed the changes to the staging server :
Through the http server I get this error :
NameError in App::MyController#index
Uninitialized constant MyActiveRecordModel::PgSearch
(Normally, this pg_search gem which is included in the GemFile should have its lib/*.rb files included in the autoload search path and a clause like load "pg_search.rb", require"pg_search" or "include PgSearch" (module included in pg_search.rb file) should pass.
In order to find clues to fix the bug, I have :
-tried if an other module provides by the gem could be included ... It works
After been to the current release path of the staging server I run "bundle exec rails c staging" and tried to :
see if the ActiveRecord Model ( which I included PgSearch) instanciation works.
see if the Module provided by the gem could be found / loaded on the server
and I have executed - include PgSearch and require "pg_search" and load "pg_search.rb".
All these commands were a succeess.
I run out of ideas to find some other clues, would you have any suggestions please ?
Thank you.
Restart the rails server
Gems introduce new code that rails will need to access. To make the new code available to Rails we go through 3 steps:
Change the Gemfile
bundle install
Restart the rails server
When do I need to restart the rails server?
It is easy to forget a step.

about rails plugins/engines

I am looking at rails plugins to help me modularize my application. I have some basic questions that I am confused about.
Can a rails plugin have its own DB? My application is very little traffic, for internal use, so I am fine with the idea of separate sqlite DB's for each plugin. When I do a "rails plugin new" even if I use --full, there is no database.yml generated. If I create one and do a rake db:create, no sqlite db is created.
Is there a good tutorial available for creating a rails plugin with rails 3.2? Most I find are older and use the enginex gem which I think is now built into rails.
Can you run your plugin as a standalone app for testing, i.e. using WEBrick? When I run "rails server" in my plugin directory, it just says "Error: Command not recognized".
I guess that's it, I am just confused on how to begin.
Creating Migrations
The Rails Guide "Getting Started with Engines" instructs you to use 'rails g model post' from the root directory for your engine.
Getting Started with Engines
If you do this, it will create the db/migrate folder for you with the migration inside of it.
$ rails g model post
invoke active_record
create db/migrate/20120517184738_create_my_engine_posts.rb
create app/models/my_engine/post.rb
invoke test_unit
create test/unit/my_engine/post_test.rb
create test/fixtures/my_engine/posts.yml
You can also generate migrations directly just the same, just as you do with a Rails app.
$ rails g migration AddMyEngineTable
invoke active_record
create db/migrate/20120517185241_add_my_engine_table.rb
Running Rails Server
The Rails Guide also states to run 'rails s' from test/dummy, not from the root of your engine directory.
I see that from an ASCIICast on the subject which covered Rails 3.1 RC5 that you used to be able to run 'rails s' from the root directory of your engine/gem. This is no longer the case.
From the Rails issue posted on Github three months ago it appears that they needed to keep the scope of the engine separate from the scope of the dummy app.
Issue #4894: Mountable Engines Rails File
In short run from the engine root:
test/dummy/script/rails s

Resources