Error regarding ActiveAdmin in Ruby on Rails when customizing the menu - ruby-on-rails

Mac OS X 10.7.3 Lion,
Ruby 1.9.2,
Rails 3.2.2,
Sass 3.2.3
Following this tutorial:
http://activeadmin.info/documentation.html
Following this video tutorial
http://www.youtube.com/watch?v=tAxlrHcEg9U
I add the activeadmin gem, run bundle install, then run
rails generate active_admin:install
rails generate active_admin:resource POST
Only after creating the app/admin/posts.rb and trying to run either
db migrate
rails server
fails with the error
uninitialized constant Post NameError
with out that posts.rb file i am able to run the admin interface error free.
I tried moving the sass-rails gem out side of the :assets in my gem file and re-running bundle install as suggested in another question, but to no avail I still have the error
according to the getting started active admin tutorial "Post" is suppose to be a module name so i assume the code above is calling a class method (ActiveAdmin as the class, register as the method) and sending the module as a parameter and the block do end
Regardless the error is implying that RoR doesn't know what Post is. As if it does not exist. Being new to rails i do not know how to navigate well, meaning i do not even know where this ActiveAdmin source file is in order to dig through it for a method Post
Thank you for the consideration and your time, I appreciate it.

The linked tutorial assumes that you have already created a model named Post (and have run rake db:migrate to link it to the database). The purpose of the rails generate active_admin:resource Post command is to tell ActiveAdmin that you want it to consider the Post model in part of what it does.
Historically, you'll see models like Post and User in Rails a lot -- these are the commonly used examples of creating a blogging application (a user can create blog posts).
So, whatever models you have in your application can be registered with ActiveAdmin by replacing Post with the name of your model.
Another note: while generators like this tend to be forgiving, a Post is a model that is defined in post.rb and is linked to a SQL table called posts. Be careful with things like upper- and lower-case, and singular and plurals. In Rails they all fit together in a special way.

Related

Rails hooking a custom generator into scaffold generator

In a new Rails 6.1.3 project, I'm attempting to have bin/rails generate scaffold Thing name:string invoke the typical Rails scaffolding (including active_record and scaffold_controller) as well as invoking my own custom generator to create an additional ThingForm that I want to be a part of my scaffolding.
I created a custom generator for form that works just fine by itself. bin/rails generate form Thing name:string is properly setting up an app/forms/thing_form.rb.
But in attempting to get this generator hooked into the default scaffolding generator, I'm running into different problems with different approaches I've tried.
Inspired by active_model_serializers, when I set up a hook_for :form as part of Rails::Generators::ResourceGenerator it ends up no longer invoking active_record. Here's the commit from my example repo: https://github.com/diachini/generator_hook_investigation/commit/8989b8f96b05ac1157d1d3a247bee9f0f2befe5d
To confirm I wasn't crazy, I have another branch that just confirmed generating a scaffold with active_model_serializers installed properly keeps the active_record invocation happening.
Taking a page out of this Gist explaining custom generators - I similarly attempted a hook_foron the ScaffoldGenerator and the ControllerGenerator
Just like my active_model_serializers attempt, this no longer invoked active_record.
It also began ignoring config for turning off assets and scaffold_stylesheet.
On a separate branch in my example repo: https://github.com/diachini/generator_hook_investigation/commit/86bc7b53ac34ea29d5e32d07486c73d962f86247
I saw a similar StackOverflow question with an update regarding copying the whole ScaffoldGenerator, but was hoping to avoid that approach if there's something simple that I'm missing with the hook_for approach that active_model_serializers took.

devise ActiveRecord::StatementInvalid in Devise::SessionsController#new error

I'm a new to rails so I'm still learning some concepts.
I installed the ruby gem devise to help with adding users/database etc..
I'm getting an error when I'm trying to access paths provided by devise.
The URL for my page is
https://rubypractice-minhaja.c9users.io
And for example if I try to access
https://rubypractice-minhaja.c9users.io//users/sign_in
I get the same error mentioned in my title I've tried for a long time to look up for a solution but I'm yet to find one.
I did check rails routes and all the routes are there so I'm not sure what the issue is.
https://github.com/minhajahmed1/event_platform
Above is the link to my github if that helps. I would really appreciate any help I get, thanks.
The line
$ rails generate devise MODEL
Was just an example, if you read the instructions you should have replaced "MODEL" with your model name "User"
$ rails generate devise User

Rails will not let me destroy scaffolding from nifty generator

I was following one of the railscasts tutorials and decided to install nifty generators. Well, being a rails noob I didn't realize that the way parameters are handled changed. Now I can't undo any of my changes. So far I managed to roll back the database but every time I try to run
rails destroy nifty:scaffold mymodel
I get the error message
attr_accessible is extracted out of rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to your Gemfile to use old one.
So I did. I added
gem 'protected_attributes'
and ran
bundle install
Then I tried to destroy it and it errored out again. I really hope nifty didn't just screw up my project. Can anyone help?
Um, this not a real solution, but a possible workaround: if the output of the rails generate nifty:scaffold mymodel command is still in your terminal buffer, you could manually delete the files it created.
And if the output isn't available, you could do rails generate nifty:scaffold mymodel2 in order to see what files nifty:scaffold created before manually deleting the corresponding files for mymodel.
Not elegant, but it might get you over the hump.

Ruby Gem Development - How to use ActiveRecord?

I'm currently trying to develop my first ruby gem and I'm already stuck. I used the "bundle gem" command to create the basic structure and read some tutorials but what I can't find is how to integrate ActiveRecord.
Where do I create my migrations?
Do I create the "db/migrations" folder within the lib folder or at the root?
And do I have to do anything in the Rakefile (I found some questions where the answer was something like "you have to create your own [my_gem]:db:migrate" or something like that.)
All I need is a way to create a gem, which defines ActiveRecord models (including the migrations of course), which can then be used by a rails app.
Any help on that one would be greatly appreciated!
Greetings, Flo
When building a gem to integrate with a rails project, you want to build a railtie engine. If you are using rails 3.0.x, use enginex, if you are using rails 3.1 you should be use the new generator:
rails g plugin new your-plugin-name
Then, inside your gem, you can just define models, inside the app/models/ folder and they will automatically be picked up.
Migrations is somewhat harder: for rails 3.1 it is ok if you define them in the correct folder, in rails 3.0 you will have to manually generate a task to copy the migrations to your code-base. Check this link where I answered that very question.
For more information on rails engines check this and this article.
getting the functionality of ActiveRecord can be done by:
require "rubygems"
require "active_record"
class User < ActiveRecord::Base
end
This should work.

Any working tutorials for Authlogic?

I've been trying to build my first rails app and have gotten stuck on the issue of user authentication. I've found a number of tutorials for using various plug-ins to do this, but so far every single one of them is out-dated, and as a result, broken!
From what I've read, I think Authlogic may be the best fit for me, and I've tried two things:
1) Going through Railscast, episode #160 (which is a tutorial for setting it up)
2) Using Ryan B's nifty_authentication gem with the --authlogic tag
In both cases, I get the following error as soon as I try to do anything with a user:
undefined local variable or method `acts_as_authentic' for #
I believe this is from the User model:
class User < ActiveRecord::Base
acts_as_authentic
end
I'm sure I've installed the authlogic gem, and I've added
config.gem "authlogic"
to my environment.rb
Any ideas about what's wrong? Anybody know of a complete and up to date tutorial for adding user authentication?
Edit:
I'm running Ruby v. 1.8.6 and rails v. 2.3.5
There is one thing that Ryan Bates in the RailsCasts episode doesn't talk about is about creating sessions table in your database. Type rake db:sessions:create in the console and run the migration rake db:migrate. Also like ghoppe says run rake gems:install after installing the gem. That is a requisite.
Here's an example app with a step-by-step guide - it's from last year but should still be mostly if not entirely accurate:
authlogic_example
Since you added that line to your environment.rb, have you tried rake gems:install to ensure the gem is installed and working correctly?
Also, what version of Ruby? What version of Rails? Have you tried running gem environment and gem list to make sure they're installed and running from the right place?
Another option is to use authlogic as a plugin, with:
script/plugin install git://github.com/binarylogic/authlogic.git
It also helps to look at a projects that uses authlogic as authentication module, like the fat_free_crm project, have a look at user.rb there
Last but not least, there is an active mailing list:
authlogic mailing list
Becoming popular is also the devise gem. Here you can add authentication with script/generate devise and you will have some views for login as well.
I forked that authlogic_example and added activity_tracker, authlogic, paperclip for user profile images, declarative_authorization, and user to user messages.
http://github.com/jspooner/authlogic_cucumber_rspec_example

Resources